Ejemplo n.º 1
0
        public static void OnAssemblyLoaded(string path)
        {
            MOD_FOLDER = Path.GetDirectoryName(path).Replace("\\", "/");
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;


            GAME_ROOT       = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) + @"/../../";
            GAMEDATA_FOLDER = Path.Combine(GAME_ROOT, "gamedata").Replace("\\", "/") + "/";
            MODS_FOLDER     = MOD_FOLDER + @"/../../";
            ICON_PATH       = Path.Combine(MOD_FOLDER, "icons").Replace("\\", "/") + "/";
            MESH_PATH       = Path.Combine(MOD_FOLDER, "Meshes").Replace("\\", "/") + "/";
            ModInfo         = JSON.Deserialize(MOD_FOLDER + "/modInfo.json")[0];

            APILogger.Log("Found mod in {0}", MOD_FOLDER);
            APILogger.Log("GAME_ROOT in {0}", GAME_ROOT);
            APILogger.Log("GAMEDATA_FOLDER in {0}", GAMEDATA_FOLDER);
            APILogger.Log("MODS_FOLDER in {0}", MODS_FOLDER);

            List <string> allinfos = new List <string>();

            DirSearch(MODS_FOLDER, "*modInfo.json", allinfos);

            foreach (var info in allinfos)
            {
                var modJson = JSON.Deserialize(info)[0];

                APILogger.Log("ModInfo Found: {0}", info);
                AllModInfos[new FileInfo(info).Directory.FullName] = modJson;
            }

            GenerateBuiltinBlocks();
        }
Ejemplo n.º 2
0
        public static void OnAssemblyLoaded(string path)
        {
            MOD_FOLDER = Path.GetDirectoryName(path);
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            APILogger.Log("Found mod in {0}", MOD_FOLDER);

            GAME_ROOT       = path.Substring(0, path.IndexOf("gamedata")).Replace("/", "/");
            GAMEDATA_FOLDER = path.Substring(0, path.IndexOf("gamedata") + "gamedata".Length).Replace("/", "/") + "/";
            MODS_FOLDER     = GAMEDATA_FOLDER + "mods/";
            ICON_PATH       = Path.Combine(MOD_FOLDER, "icons").Replace("\\", "/") + "/";
            MESH_PATH       = Path.Combine(MOD_FOLDER, "Meshes").Replace("\\", "/") + "/";
            ModInfo         = JSON.Deserialize(MOD_FOLDER + "/modInfo.json")[0];

            List <string> allinfos = new List <string>();

            DirSearch(MODS_FOLDER, "*modInfo.json", allinfos);

            foreach (var info in allinfos)
            {
                var modJson = JSON.Deserialize(info)[0];

                if (modJson.TryGetAs("enabled", out bool isEnabled) && isEnabled)
                {
                    APILogger.Log("ModInfo Found: {0}", info);
                    AllModInfos[new FileInfo(info).Directory.FullName] = modJson;
                }
            }

            GenerateBuiltinBlocks();
        }
Ejemplo n.º 3
0
        public static JSONNode JsonSerialize <T>(this T obj)
        {
            var objStr = JsonConvert.SerializeObject(obj, Formatting.None, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            APILogger.LogToFile(objStr);
            var json = JSON.DeserializeString(objStr);

            return(json);
        }
Ejemplo n.º 4
0
        public static Vector3Int GetBlockOffset(this Vector3Int vector, BlockSide blockSide)
        {
            var vectorValues = blockSide.GetAttribute <BlockSideVectorValuesAttribute>();

            if (vectorValues != null)
            {
                return(vector.Add(vectorValues.X, vectorValues.Y, vectorValues.Z));
            }
            else
            {
                APILogger.Log(ChatColor.yellow, "Unable to find BlockSideVectorValuesAttribute for {0}", blockSide.ToString());
                return(vector);
            }
        }
Ejemplo n.º 5
0
        public static Dictionary <string, List <string> > GetJSONSettingPaths(string fileType)
        {
            Dictionary <string, List <string> > retval = new Dictionary <string, List <string> >();

            try
            {
                foreach (var info in GameInitializer.AllModInfos)
                {
                    if (info.Value.TryGetAs(GameInitializer.NAMESPACE + ".jsonFiles", out JSONNode jsonFilles))
                    {
                        foreach (var jsonNode in jsonFilles.LoopArray())
                        {
                            if (jsonNode.TryGetAs("fileType", out string jsonFileType))
                            {
                                if (jsonFileType == fileType)
                                {
                                    if (jsonNode.TryGetAs("relativePath", out string itemsPath))
                                    {
                                        if (!retval.ContainsKey(info.Key))
                                        {
                                            retval.Add(info.Key, new List <string>());
                                        }

                                        retval[info.Key].Add(itemsPath);
                                        APILogger.LogToFile("Getting json configurations {0} from file {1} Path {2}", fileType, info.Key, itemsPath);
                                    }
                                    else
                                    {
                                        APILogger.Log(ChatColor.red, "Unable to read relativePath for fileType {0} from file {1}", itemsPath, info.Key);
                                    }
                                }
                            }
                            else
                            {
                                APILogger.Log(ChatColor.red, "Unable to read fileType from file {0}", info.Key);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                APILogger.LogError(ex);
            }

            return(retval);
        }
Ejemplo n.º 6
0
        private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            APILogger.Log(args.Name);
            try
            {
                if (args.Name.Contains("System.Xml.Linq"))
                {
                    return(Assembly.LoadFile(MOD_FOLDER + "/System.Xml.Linq.dll"));
                }

                if (args.Name.Contains("System.ComponentModel.DataAnnotations"))
                {
                    return(Assembly.LoadFile(MOD_FOLDER + "/System.ComponentModel.DataAnnotations.dll"));
                }

                if (args.Name.Contains("System.Numerics"))
                {
                    return(Assembly.LoadFile(MOD_FOLDER + "/System.Numerics.dll"));
                }

                if (args.Name.Contains("System.Runtime.Serialization"))
                {
                    return(Assembly.LoadFile(MOD_FOLDER + "/System.Runtime.Serialization.dll"));
                }

                if (args.Name.Contains("System.Transactions"))
                {
                    return(Assembly.LoadFile(MOD_FOLDER + "/System.Transactions.dll"));
                }

                if (args.Name.Contains("System.Data.SQLite"))
                {
                    return(Assembly.LoadFile(MOD_FOLDER + "/System.Data.SQLite.dll"));
                }

                if (args.Name.Contains("System.Data"))
                {
                    return(Assembly.LoadFile(MOD_FOLDER + "/System.Data.dll"));
                }
            }
            catch (Exception ex)
            {
                APILogger.LogError(ex);
            }

            return(null);
        }
Ejemplo n.º 7
0
        public static bool TryGetCraftJobSettings(this BlockEntities.BlockEntityCallbacks callbacks, string name, out CraftingJobRotatedSettings craftingJobSettings)
        {
            craftingJobSettings = null;

            var craftJobInstance = callbacks.AutoLoadedInstances.FirstOrDefault(o => o is BlockJobManager <CraftingJobInstance> manager && manager.Settings is CraftingJobRotatedSettings set && set.NPCTypeKey == name) as BlockJobManager <CraftingJobInstance>;

            if (craftJobInstance == null)
            {
                APILogger.Log(ChatColor.yellow, "Unable to find craft lit job settings for {0}", name);
            }
            else
            {
                craftingJobSettings = craftJobInstance.Settings as CraftingJobRotatedSettings;
            }

            return(craftingJobSettings != null);
        }
Ejemplo n.º 8
0
        public static bool TryGetGuardJobSettings(this BlockEntities.BlockEntityCallbacks callbacks, string name, out GuardJobSettings guardJobSettings)
        {
            guardJobSettings = null;

            var guardJobInstance = callbacks.AutoLoadedInstances.Where(o => o is BlockJobManager <GuardJobInstance> manager && manager.Settings is GuardJobSettings set && set.NPCTypeKey == name).FirstOrDefault() as BlockJobManager <GuardJobInstance>;

            if (guardJobInstance == null)
            {
                APILogger.Log(ChatColor.yellow, "Unable to find guard job settings for {0}", name);
            }
            else
            {
                guardJobSettings = guardJobInstance.Settings as GuardJobSettings;
            }

            return(guardJobSettings != null);
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     Clears any queued items and stops processing the queue.
        /// </summary>
        public void Stop()
        {
            try
            {
                _running = false;

                lock (_executeRequests)
                    _executeRequests.Clear();

                _tokenSource.Cancel();

                _shutdownSemaphore.WaitOne(5000); // wait up to 5 seconds for the QueueManager finish.

                lock (_runningTasks)
                    _runningTasks.Clear();
            }
            catch (Exception ex)
            {
                APILogger.LogError(ex);
            }
        }
Ejemplo n.º 10
0
        private void QueueManager()
        {
            while (_running && !_tokenSource.IsCancellationRequested)
            {
                try
                {
                    _processQueueSemaphore.WaitOne(1000);
                }
                catch { } // log no error in the case that this thread is aborted.

                try
                {
                    while (_executeRequests.Count > 0 && !_tokenSource.IsCancellationRequested)
                    {
                        T    request = _defaultValue;
                        Task newTask = null;

                        // Before we dequeue a request we check to ensure we can process it.
                        // loop over all our workers to find one that is not busy.
                        lock (_runningTasks)
                        {
                            foreach (Task worker in _runningTasks)
                            {
                                if (!worker.IsCompleted)
                                {
                                    continue;
                                }

                                _runningTasks.Remove(worker);
                                break;
                            }

                            if (_runningTasks.Count < _maxWorkers)
                            {
                                try
                                {
                                    // Get the next request.
                                    lock (_executeRequests)
                                        request = _executeRequests.Dequeue();
                                }
                                catch { } // log no error in the case there is nothing to dequeue

                                if (request != null && !request.Equals(_defaultValue))
                                {
                                    newTask = _taskFoctory.StartNew(() =>
                                    {
                                        try
                                        {
                                            DoWork?.Invoke(this, request);
                                        }
                                        catch (Exception ex)
                                        {
                                            APILogger.LogError(ex, "Error on invoking Do work in queue factory. {0}", GetName());
                                        }
                                    });

                                    _runningTasks.Add(newTask);
                                }
                            }
                        }

                        if (newTask == null)
                        {
                            _allWorkersBusySemaphore.WaitOne(100); // so we do not get into a tight loop if all workers are busy.
                        }
                    }
                }
                catch (Exception ex)
                {
                    APILogger.LogError(ex);
                }
            }

            _shutdownSemaphore.Set();
        }