<commit_before>// Copyright 2014 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package engi

import (
	"math"
	"path"
	"path/filepath"

	"github.com/ajhager/webgl"
)

type Resource struct {
	kind string
	name string
	url  string
}

type Loader struct {
	resources []Resource
	loaded    map[Resource]struct{}
	images    map[string]*Texture
	jsons     map[string]string
	sounds    map[string]*Sound
}

func NewLoader() *Loader {
	return &Loader{
		resources: make([]Resource, 1),
		loaded:    make(map[Resource]struct{}),
		images:    make(map[string]*Texture),
		jsons:     make(map[string]string),
		sounds:    make(map[string]*Sound),
	}
}

func (l *Loader) Add(name, url string) {
	kind := path.Ext(url)[1:]
	url = filepath.FromSlash(url)
	l.resources = append(l.resources, Resource{kind, name, url})
}

func (l *Loader) Image(name string) *Texture {
	return l.images[name]
}

func (l *Loader) Json(name string) string {
	return l.jsons[name]
}

func (l *Loader) Sound(name string) *Sound {
	return l.sounds[name]
}

func (l *Loader) Replace(name, url string) {
	kind := path.Ext(url)[1:]
	url = filepath.FromSlash(url)
	r := Resource{kind, name, url}
	switch kind {
	case "wav", "flac":
		if old, ok := l.sounds[name]; ok {
			old.Delete()
		}
		if data, err := loadSound(r); err == nil {
			l.sounds[r.name] = data
			l.loaded[r] = struct{}{}
		} else {
			panic(err)
		}
	}
}

func (l *Loader) Load(onFinish func()) {
	for _, r := range l.resources {
		if _, loaded := l.loaded[r]; loaded {
			// don't load stuff twice
			continue
		}
		switch r.kind {
		case "png":
			data, err := loadImage(r)
			if err == nil {
				l.images[r.name] = NewTexture(data)
				l.loaded[r] = struct{}{}
			}
		case "json":
			data, err := loadJson(r)
			if err == nil {
				l.jsons[r.name] = data
				l.loaded[r] = struct{}{}
			}
		case "wav", "flac":
			data, err := loadSound(r)
			if err == nil {
				l.sounds[r.name] = data
				l.loaded[r] = struct{}{}
			} else {
				panic(err)
			}
		}
	}
	onFinish()
}

type Image interface {
	Data() interface{}
	Width() int
	Height() int
}

func LoadShader(vertSrc, fragSrc string) *webgl.Program {
	vertShader := gl.CreateShader(gl.VERTEX_SHADER)
	gl.ShaderSource(vertShader, vertSrc)
	gl.CompileShader(vertShader)
	defer gl.DeleteShader(vertShader)

	fragShader := gl.CreateShader(gl.FRAGMENT_SHADER)
	gl.ShaderSource(fragShader, fragSrc)
	gl.CompileShader(fragShader)
	defer gl.DeleteShader(fragShader)

	program := gl.CreateProgram()<commit_msg>panic on asset loading errors<commit_after>// Copyright 2014 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package engi

import (
	"math"
	"path"
	"path/filepath"

	"github.com/ajhager/webgl"
)

type Resource struct {
	kind string
	name string
	url  string
}

type Loader struct {
	resources []Resource
	loaded    map[Resource]struct{}
	images    map[string]*Texture
	jsons     map[string]string
	sounds    map[string]*Sound
}

func NewLoader() *Loader {
	return &Loader{
		resources: make([]Resource, 1),
		loaded:    make(map[Resource]struct{}),
		images:    make(map[string]*Texture),
		jsons:     make(map[string]string),
		sounds:    make(map[string]*Sound),
	}
}

func (l *Loader) Add(name, url string) {
	kind := path.Ext(url)[1:]
	url = filepath.FromSlash(url)
	l.resources = append(l.resources, Resource{kind, name, url})
}

func (l *Loader) Image(name string) *Texture {
	return l.images[name]
}

func (l *Loader) Json(name string) string {
	return l.jsons[name]
}

func (l *Loader) Sound(name string) *Sound {
	return l.sounds[name]
}

func (l *Loader) Replace(name, url string) {
	kind := path.Ext(url)[1:]
	url = filepath.FromSlash(url)
	r := Resource{kind, name, url}
	switch kind {
	case "wav", "flac":
		if old, ok := l.sounds[name]; ok {
			old.Delete()
		}
		data, err := loadSound(r)
		fatalErr(err)
		l.sounds[r.name] = data
		l.loaded[r] = struct{}{}
	}
}

func (l *Loader) Load(onFinish func()) {
	for _, r := range l.resources {
		if _, loaded := l.loaded[r]; loaded {
			// don't load stuff twice
			continue
		}
		switch r.kind {
		case "png":
			data, err := loadImage(r)
			fatalErr(err)
			l.images[r.name] = NewTexture(data)
			l.loaded[r] = struct{}{}
		case "json":
			data, err := loadJson(r)
			fatalErr(err)
			l.jsons[r.name] = data
			l.loaded[r] = struct{}{}
		case "wav", "flac":
			data, err := loadSound(r)
			fatalErr(err)
			l.sounds[r.name] = data
			l.loaded[r] = struct{}{}
		}
	}
	onFinish()
}

type Image interface {
	Data() interface{}
	Width() int
	Height() int
}

func LoadShader(vertSrc, fragSrc string) *webgl.Program {
	vertShader := gl.CreateShader(gl.VERTEX_SHADER)
	gl.ShaderSource(vertShader, vertSrc)
	gl.CompileShader(vertShader)
	defer gl.DeleteShader(vertShader)

	fragShader := gl.CreateShader(gl.FRAGMENT_SHADER)
	gl.ShaderSource(fragShader, fragSrc)
	gl.CompileShader(fragShader)
	defer gl.DeleteShader(fragShader)

	program := gl.CreateProgram()
{'fraction_non_alphanumeric': 0.15219107777339125, 'fraction_numerical': 0.0027635215159889457, 'mean_word_length': 3.717877094972067, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2385644', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1878, 'n_words': 465}