Example #1
0
        public static void AddLuaObjectComponent(string id, string type)
        {
            var go = GameObject.Find(id);

            if (go == null)
            {
                InGameDebug.Log("Couldn't find GameObject with name '" + id + "'.");
                return;
            }
            var luaComponent = GameObject.Find(id).AddComponent <LuaObjectComponent>();

            luaComponent.Load(type);
        }
Example #2
0
        private void Awake()
        {
            _canvas = FindObjectsOfType <Canvas>().Where(
                canvas => canvas.GetComponentsInChildren <DraggableUIElement>().Contains(this)
                ).First();

            if (_canvas == null)
            {
                InGameDebug.Log("<color=red>" + name + ": ResizableUIElement couldn't find its canvas.</color>");
            }

            _rectTransform       = GetComponent <RectTransform>();
            _parentRectTransform = _rectTransform.parent.GetComponent <RectTransform>();
        }
Example #3
0
        public static void SetFontSize(int id, int size)
        {
            var go   = ObjectBuilder.Get(id);
            var text = go.GetComponentInChildren <Text>();

            if (text == null)
            {
                InGameDebug.Log("GameObject has no Text component, nor have any of its children.");
            }
            else
            {
                text.fontSize = size;
            }
        }
Example #4
0
        public static void SetTextColor(int id, int r, int g, int b, int a)
        {
            var go   = ObjectBuilder.Get(id);
            var text = go.GetComponentInChildren <Text>();

            if (text == null)
            {
                InGameDebug.Log("GameObject has no Text component, nor have any of its children.");
            }
            else
            {
                text.color = new Color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
            }
        }
Example #5
0
        public static int CreateAudioSource(string clipKey)
        {
            var source = GameObject.Instantiate(new GameObject()).AddComponent <AudioSource>();
            var clip   = StreamingAssetsDatabase.GetSound(clipKey);

            if (clip == null)
            {
                InGameDebug.Log("Couldn't find clip " + clipKey + ".");
            }
            source.clip            = clip;
            _audioSources[_nextId] = source;
            _unmodifiedAudioSourceVolumes[source] = 1.0f;
            return(_nextId);
        }
Example #6
0
        public static void SetFont(int id, string fontName)
        {
            var go   = ObjectBuilder.Get(id);
            var text = go.GetComponentInChildren <Text>();

            if (text == null)
            {
                InGameDebug.Log("GameObject has no Text component, nor have any of its children.");
            }
            else
            {
                text.font = Resources.Load <Font>("Fonts/" + fontName);
            }
        }
Example #7
0
        public static void SetText(int id, string str)
        {
            var go   = ObjectBuilder.Get(id);
            var text = go.GetComponentInChildren <Text>();

            if (text == null)
            {
                InGameDebug.Log("GameObject has no Text component, nor have any of its children.");
            }
            else
            {
                text.text = str;
            }
        }
Example #8
0
        public static void Set(int id, string key, DynValue value)
        {
            if (!_audioSources.ContainsKey(id))
            {
                InGameDebug.Log("SoundManager.Set(): No AudioSource with id " + id + ".");
                return;
            }
            var audioSource = _audioSources[id];

            switch (key.ToLower())
            {
            case "clip":
                audioSource.clip = StreamingAssetsDatabase.GetSound(value.String);
                break;

            case "loop":
                audioSource.loop = value.Boolean;
                break;

            case "volume":
                _unmodifiedAudioSourceVolumes[audioSource] = (float)value.Number;
                if (_musicSources.ContainsKey(id))
                {
                    audioSource.volume = _unmodifiedAudioSourceVolumes[audioSource] * _musicVolume;
                }
                if (_musicSources.ContainsKey(id))
                {
                    audioSource.volume = _unmodifiedAudioSourceVolumes[audioSource] * _effectsVolume;
                }
                break;

            case "pitch":
                audioSource.pitch = (float)value.Number;
                break;

            case "mute":
                audioSource.mute = value.Boolean;
                break;

            case "time":
                audioSource.time = (float)value.Number;
                break;
            }
        }
Example #9
0
 public static Texture2D GetTexture(string key)
 {
     if (_textures.ContainsKey(key))
     {
         return(_textures[key]);
     }
     else
     {
         InGameDebug.Log("Missing texture: '" + key + "'.");
         if (_textures.ContainsKey("Misc.Missing"))
         {
             return(_textures["Misc.Missing"]);
         }
         else
         {
             return(Resources.Load <Texture2D>("Textures/Missing"));
         }
     }
 }
Example #10
0
 public static string GetString(string key)
 {
     if (_defs.ContainsKey(key))
     {
         var tydString = _defs[key] as TydString;
         if (tydString != null)
         {
             return(tydString.Value);
         }
         else
         {
             InGameDebug.Log("Node " + key + " is not a TydString.");
             return(null);
         }
     }
     else
     {
         InGameDebug.Log("Missing def: '" + key + "'.");
         return(null);
     }
 }
Example #11
0
 private void Start()
 {
     if (_start == null)
     {
         Debug.LogError(name + ": Start is null. ", this);
     }
     if (_update == null)
     {
         Debug.LogError(name + ": Update is null.", this);
     }
     if (_start != null)
     {
         try
         {
             Script.Call(_start, Table, Id);
         }
         catch (ScriptRuntimeException e)
         {
             InGameDebug.Log("<color=red>Oh no.</color> " + e.DecoratedMessage);
         }
     }
 }
Example #12
0
        public static void LoadScriptsFromDirectory(string directory)
        {
            InGameDebug.Log("Loading scripts...");
            Script.DefaultOptions.ScriptLoader = new YeeterScriptLoader();
            var folders = new List <string>()
            {
                directory
            };

            folders.AddRange(GetAllSubDirectories(directory));
            foreach (var folder in folders)
            {
                InGameDebug.Log("\tSearching '" + folder + "'...");
                foreach (var file in Directory.GetFiles(folder))
                {
                    InGameDebug.Log("\tFile found: '" + file + "'.");
                    if (Path.GetExtension(file) == ".lua")
                    {
                        var pathRelativeToScriptsFolder =
                            file
                            .Replace(".lua", "")
                            .Remove(0, directory.Length + 1);
                        var key =
                            pathRelativeToScriptsFolder
                            .Replace('\\', '.')
                            .Replace('/', '.');
                        InGameDebug.Log(
                            "\t\t<color=green>Script loaded: '" + key + "' (no module).</color>");
                        YeeterScriptLoader.AssetDatabaseToPath[key] = file;
                    }
                    else
                    {
                        InGameDebug.Log("\t\tInvalid extension: '" + Path.GetExtension(file) + "'. Skipping.");
                    }
                }
            }
            InGameDebug.Log("Scripts loaded.");
        }
Example #13
0
 private static DynValue AddObject(GameObject gameObject)
 {
     _objects.Add(_nextId, gameObject);
     _ids.Add(gameObject, _nextId);
     foreach (var child in gameObject.transform)
     {
         var go = child as GameObject;
         if (go == null)
         {
             continue;
         }
         if (!_ids.ContainsKey(go))
         {
             AddObject(go);
         }
     }
     gameObject.name = gameObject.name.Replace("(Clone)", "");
     if (InGameDebug.LogObjectCreated)
     {
         InGameDebug.Log("Created GameObject '" + gameObject.name + "' with id " + _nextId + ".");
     }
     return(DynValue.NewNumber(_nextId++));
 }
Example #14
0
 private void Update()
 {
     if (_update != null)
     {
         try
         {
             if (LuaManager.LoadUpdateEachFrame)
             {
                 var _table = Script.DoFile(Path).Table;
                 _update = _table.Get("Update").Function;
                 Script.Call(_update, _table, Id, Time.deltaTime);
             }
             else
             {
                 Script.Call(_update, Table, Id, Time.deltaTime);
             }
         }
         catch (ScriptRuntimeException e)
         {
             InGameDebug.Log("<color=red>Oh no.</color> " + e.DecoratedMessage);
         }
     }
 }
Example #15
0
        public static void LoadActiveModules()
        {
            // Load contents of mods in load order
            InGameDebug.Log("Loading active mods...");
            var loadOrderFile = Application.streamingAssetsPath + "/Settings/LoadOrder.txt";

            if (!File.Exists(loadOrderFile))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(loadOrderFile));
                File.WriteAllText(loadOrderFile, _defaultCompanyName + ".Core");
            }
            var loadOrder     = File.ReadAllLines(loadOrderFile);
            var activeModules = new List <Module>();

            foreach (string line in loadOrder)
            {
                if (_packageIdToModule.ContainsKey(line.Trim()))
                {
                    InGameDebug.Log("\t" + line);
                    activeModules.Add(_packageIdToModule[line]);
                }
                else
                {
                    InGameDebug.Log(
                        "\t" + line + " was present in " +
                        "StreamingAssets/Settings/LoadOrder.txt but is not installed or its " +
                        "About.xml is invalid."
                        );
                }
            }
            InGameDebug.Log("Active mods loaded.");
            if (activeModules.Count == 0)
            {
                InGameDebug.Log("No active mods?");
            }
            ActiveModules = activeModules.ToArray();
        }
Example #16
0
        private void OnCommandEntered(string code)
        {
            _commandsAbove.Push(code);
            _text.text  = "";
            _field.text = "";
            if (EventSystem.current.currentSelectedGameObject != gameObject)
            {
                return;
            }
            _field.OnSelect(new BaseEventData(EventSystem.current));
            if (code == null || code == "")
            {
                return;
            }

            InGameDebug.Log("> " + code);

            // Check if this is a custom command.
            string directCommand = code.ToLowerInvariant().Trim();

            if (directCommand == "help" || directCommand == "?")
            {
                InGameDebug.Help();
            }
            else if (directCommand == "clear")
            {
                InGameDebug.Clear();
            }
            else
            {
                // Lua script, let LuaManager handle this.
                LuaManager.DoString(code);
                _commandsBelow = new Stack <string>();
            }
            OnCommandDone?.Invoke(code);
        }
Example #17
0
        private IEnumerator <UnityWebRequestAsyncOperation> StreamFile(AudioMetaData file)
        {
            var path = file.Path.Replace('\\', '/');

            using (var www = UnityWebRequestMultimedia.GetAudioClip("file://" + path, GetAudioType(path)))
            {
                yield return(www.SendWebRequest());

                if (www.isNetworkError)
                {
                    InGameDebug.Log(www.error);
                }
                else
                {
                    var clip = DownloadHandlerAudioClip.GetContent(www);
                    InGameDebug.Log("\t\t<color=green>AudioLoader: '" + path + "' loaded.</color>");
                    Clips.Add(file, clip);
                    if (Clips.Count == _count)
                    {
                        OnLoadingDone?.Invoke();
                    }
                }
            }
        }
Example #18
0
        public static bool LoadSoundsFromActiveModules()
        {
            InGameDebug.Log("Loading sounds...");
            if (ActiveModules == null || ActiveModules.Length == 0)
            {
                InGameDebug.Log(
                    "StreamingAssetsDatabase.LoadSoundsFromActiveModules(): " +
                    "No active modules. Did you forget to call LoadActiveModules()?");
                return(false);
            }

            var audioFiles = new Dictionary <string, AudioLoader.AudioMetaData>();

            foreach (var mod in ActiveModules)
            {
                InGameDebug.Log("\t" + mod.Id);
                var folders = new List <string>()
                {
                    mod.Path + "/Sounds"
                };
                if (!Directory.Exists(folders[0]))
                {
                    InGameDebug.Log("\t" + mod.Id + ": No Sounds folder.");
                    continue;
                }
                folders.AddRange(GetAllSubDirectories(mod.Path + "/Sounds"));
                foreach (var folder in folders)
                {
                    InGameDebug.Log("\tSearching '" + folder + "'...");
                    foreach (var file in Directory.GetFiles(folder))
                    {
                        InGameDebug.Log("\tFile found: '" + file + "'.");
                        if (Path.GetExtension(file) == ".ogg" || Path.GetExtension(file) == ".wav")
                        {
                            var pathRelativeToSoundsFolder =
                                file
                                .Replace(".wav", "")
                                .Replace(".ogg", "")
                                .Remove(0, mod.Path.Length + "/Sounds".Length + 1);
                            var key =
                                pathRelativeToSoundsFolder
                                .Replace('\\', '.')
                                .Replace('/', '.');
                            InGameDebug.Log("<color=green>\t\tValid file. Set to be loaded.</color>");
                            if (audioFiles.ContainsKey(key))
                            {
                                InGameDebug.Log(
                                    "<color=green>\t\tOverrides file from " + audioFiles[key].Mod.Id + "</color>"
                                    );
                            }
                            audioFiles[key] = new AudioLoader.AudioMetaData(mod, file, key);
                        }
                        else
                        {
                            InGameDebug.Log("\t\tInvalid extension: '" + Path.GetExtension(file) + "'. Skipping.");
                        }
                    }
                }
            }

            if (audioFiles.Count > 0)
            {
                // Start streaming in audio.
                var loader = UnityEngine.Object.Instantiate(new GameObject()).AddComponent <AudioLoader>();
                loader.LoadFiles(audioFiles.Values.ToList());
                loader.OnLoadingDone += () =>
                {
                    _audioClips = new Dictionary <string, AudioClip>();
                    foreach (var pair in loader.Clips)
                    {
                        var file = pair.Key.Path;
                        var mod  = pair.Key.Mod;
                        var clip = pair.Value;
                        var pathRelativeToScriptsFolder =
                            file
                            .Replace(".wav", "")
                            .Replace(".ogg", "")
                            .Remove(0, mod.Path.Length + "/Sounds".Length + 1);
                        var key =
                            pathRelativeToScriptsFolder
                            .Replace('\\', '.')
                            .Replace('/', '.');
                        InGameDebug.Log("\t\t<color=green>Sound loaded: '" + key + "' (from " + mod.Id + ").</color>");
                        _audioClips[key] = clip;
                    }
                    InGameDebug.Log("Sounds loaded.");
                    OnSoundsDoneLoading?.Invoke();
                };
            }
            else
            {
                // No audio to load.
                return(false);
            }
            return(true);
        }
Example #19
0
        public static void LoadDefsFromDirectory(string directory)
        {
            // Visits a node to add it to the defs dictionary.
            void AddNodeToAssetDatabase(string parentKey, TydNode node, int index = 0)
            {
                if (node == null)
                {
                    return;
                }

                // Add self.
                string nodeKey = parentKey;

                // TydDocuments should have the same key as their folder. Otherwise we get
                // "Some.Folder.File.0.NodeInFile" instead of "Some.Folder.File.NodeInFile"
                if (node as TydDocument == null)
                {
                    nodeKey += ".";
                    nodeKey += node.Name != null ? node.Name : index.ToString();
                }
                _defs[nodeKey] = node;
                InGameDebug.Log("<color=green>\t\t\tLoaded def: " + nodeKey + "</color>");

                // Visit child nodes if this is a collection.
                var table = node as TydTable;

                if (table != null)
                {
                    for (int i = 0; i < table.Nodes.Count; i++)
                    {
                        AddNodeToAssetDatabase(nodeKey, table.Nodes[i], i);
                    }
                    return;
                }
                var list = node as TydList;

                if (list != null)
                {
                    for (int i = 0; i < list.Nodes.Count; i++)
                    {
                        AddNodeToAssetDatabase(nodeKey, list.Nodes[i], i);
                    }
                    return;
                }
            }

            // Adds a node to the Tyd inheritance list so we can do inheritance.
            void AddNodeToInheritance(TydNode node)
            {
                if (node == null)
                {
                    return;
                }

                // Add to inheritance and visit child nodes if this is a collection.

                var collection = node as TydCollection;

                if (collection != null)
                {
                    Inheritance.Register(collection);
                    for (int i = 0; i < collection.Nodes.Count; i++)
                    {
                        AddNodeToInheritance(collection[i]);
                    }
                }
            }

            // Merge files.
            var mergedFiles = new Dictionary <string, string>();
            var folders     = new List <string>()
            {
                directory
            };

            folders.AddRange(GetAllSubDirectories(directory));
            foreach (var folder in folders)
            {
                InGameDebug.Log("\t\tSearching '" + folder + "'...");
                foreach (var file in Directory.GetFiles(folder))
                {
                    InGameDebug.Log("\t\tFile found: '" + file + "'.");
                    if (Path.GetExtension(file) == Constants.TydFileExtension)
                    {
                        var pathRelativeToDefsFolder =
                            file
                            .Replace(Constants.TydFileExtension, "")
                            .Remove(0, directory.Length + 1);
                        var key =
                            pathRelativeToDefsFolder
                            .Replace('\\', '.')
                            .Replace('/', '.');
                        InGameDebug.Log("<color=green>\t\tValid def file: '" + key + "' Adding nodes.</color>");

                        if (!mergedFiles.ContainsKey(key))
                        {
                            mergedFiles[key] = "";
                        }
                        mergedFiles[key] += File.ReadAllText(file) + "\n";
                    }
                    else
                    {
                        InGameDebug.Log("\t\tInvalid extension: '" + Path.GetExtension(file) + "'. Skipping.");
                    }
                }
            }
            // Add all nodes to inheritance.
            var docs = new Dictionary <string, TydDocument>();

            Inheritance.Initialize();
            foreach (var pair in mergedFiles)
            {
                var nodes = TydFromText.Parse(pair.Value).ToList();
                var doc   = new TydDocument(nodes);
                AddNodeToInheritance(doc);
                docs[pair.Key] = doc;
            }
            Inheritance.ResolveAll();

            // Now that we've applied inheritance we can load the node's defs and have stuff work.
            foreach (var pair in docs)
            {
                AddNodeToAssetDatabase(pair.Key, pair.Value);
            }

            InGameDebug.Log("Defs loaded.");
        }
Example #20
0
        public static void LoadModules()
        {
            string dataFolder = Application.streamingAssetsPath + "/Data";
            string modsFolder = Application.streamingAssetsPath + "/Mods";

            var mods = new List <Module>();

            _packageIdToModule = new Dictionary <string, Module>();

            // Read all mods.
            InGameDebug.Log("Loading mods...");
            Module LoadModule(string path)
            {
                if (Directory.Exists(path + "/About"))
                {
                    if (File.Exists(path + "/About/About.xml"))
                    {
                        return(XmlLoader.Load <Module>(path + "/About/About.xml"));
                    }
                    else
                    {
                        InGameDebug.Log("\t\tNo About.xml");
                        return(null);
                    }
                }
                else
                {
                    Debug.Log("\t\tNo About folder.");
                    return(null);
                }
            }

            bool AddMod(string path)
            {
                Debug.Log("\t\tTrying to add mod at '" + path + "'...");
                var mod = LoadModule(path);

                if (mod != null)
                {
                    mods.Add(mod);
                    _packageIdToModule.Add(mod.Id, mod);
                    mod.Path = path;
                    InGameDebug.Log("\t\t'" + mod.Id + "' loaded.");
                    return(true);
                }
                else
                {
                    InGameDebug.Log("\t\t<color=orange>Failed to load mod at '" + path + "'.</color>");
                    return(false);
                }
            }

            if (!Directory.Exists(dataFolder))
            {
                InGameDebug.Log("No Data folder in StreamingAssets. Created one for you ;).");
                Directory.CreateDirectory(dataFolder);
            }
            bool anyDataModule = false;

            foreach (string folder in Directory.GetDirectories(dataFolder))
            {
                InGameDebug.Log("\tData folder found: '" + folder + "'.");
                bool success = AddMod(folder);
                if (success)
                {
                    anyDataModule = true;
                }
            }
            if (!anyDataModule)
            {
                // Create Core module if there are no modules in Data.
                Directory.CreateDirectory(Path.Combine(dataFolder, "Core"));
                var core = new Module();
                core.Id   = _defaultCompanyName + ".Core";
                core.Name = "Core";
                Directory.CreateDirectory(Path.Combine(dataFolder, "Core", "About"));
                var aboutFile = File.Create(Path.Combine(dataFolder, "Core", "About", "About.xml"));
                new XmlSerializer(typeof(Module)).Serialize(aboutFile, core);
                aboutFile.Close();

                InGameDebug.Log("\t<color=orange>No module found in Data. Added Core module.</color>");
            }
            if (!Directory.Exists(modsFolder))
            {
                InGameDebug.Log("No Mods folder in StreamingAssets. Created one for you ;).");
                Directory.CreateDirectory(modsFolder);
                File.WriteAllText(modsFolder + "/Place Mods Here.txt", "");
            }
            foreach (string folder in Directory.GetDirectories(modsFolder))
            {
                InGameDebug.Log("\tMod folder found: '" + folder + "'.");
                AddMod(folder);
            }
            _modules = mods.ToArray();
            InGameDebug.Log("Mods loaded.");
        }
Example #21
0
        public static void LoadSettings()
        {
            // Visits a node to add it to the defs dictionary.
            void AddSetting(string parentKey, TydNode node, int index = 0)
            {
                if (node == null)
                {
                    return;
                }

                // Add self.
                string nodeKey = parentKey;

                if (node as TydDocument == null)
                {
                    nodeKey += ".";
                    nodeKey += node.Name != null ? node.Name : index.ToString();
                    _settingsNodes[nodeKey] = node;
                }
                InGameDebug.Log("<color=green>\t\t\tLoaded setting: " + nodeKey + "</color>");
                // Also add this to settings if it is an end point.
                var tydString = node as TydString;

                if (tydString != null)
                {
                    _settings.Add(nodeKey, tydString.Value);
                }

                // Visit child nodes if this is a collection.
                var table = node as TydTable;

                if (table != null)
                {
                    for (int i = 0; i < table.Nodes.Count; i++)
                    {
                        AddSetting(nodeKey, table.Nodes[i], i);
                    }
                    return;
                }
                var list = node as TydList;

                if (list != null)
                {
                    for (int i = 0; i < list.Nodes.Count; i++)
                    {
                        AddSetting(nodeKey, list.Nodes[i], i);
                    }
                    return;
                }
            }

            InGameDebug.Log("Loading settings...");
            var file = Application.streamingAssetsPath + "/Settings/Settings.tyd";

            Directory.CreateDirectory(Path.GetDirectoryName(file));
            if (!File.Exists(file))
            {
                File.WriteAllText(file, "musicVolume 0.5\neffectsVolume 0.5");
            }
            var tydFile = TydFile.FromFile(file);

            AddSetting("Settings", tydFile.DocumentNode);
            InGameDebug.Log("Settings loaded.");
        }