Example #1
0
        public void Save()
        {
            if (RExModule.GetPath() == RExModule.PATH_NOT_FOUND)
            {
                return;
            }

            Debug.Log(string.Format("REx: Saving config at {0}", FILENAME));

            try
            {
                var xDoc     = new XmlDocument();
                var settings = xDoc.CreateElement("NetworkExtensionsSettings");

                xDoc.AppendChild(settings);

                foreach (var part in PartsEnabled)
                {
                    var xmlElem = xDoc.CreateElement(part.Key);
                    xmlElem.InnerText = part.Value.ToString();

                    settings.AppendChild(xmlElem);
                }

                xDoc.Save(FILENAME);
            }
            catch (Exception ex)
            {
                Debug.Log(string.Format("REx: Crashed saving config at {0} {1}", FILENAME, ex));
            }
        }
        //public static IEnumerable<NetInfo> Build(this INetInfoBuilder builder, ICollection<Action> lateOperations)
        //{
        //    // Ground versions

        //    var groundInfo = builder.BuildVersion(NetInfoVersion.Ground, lateOperations);
        //    var groundGrassInfo = builder.BuildVersion(NetInfoVersion.GroundGrass, lateOperations);
        //    var groundTreesInfo = builder.BuildVersion(NetInfoVersion.GroundTrees, lateOperations);

        //    var groundInfos = new[] { groundInfo, groundGrassInfo, groundTreesInfo };
        //    groundInfos = groundInfos.Where(gi => gi != null).ToArray();

        //    if (!groundInfos.Any())
        //    {
        //        yield break;
        //    }

        //    // Other versions
        //    var elevatedInfo = builder.BuildVersion(NetInfoVersion.Elevated, lateOperations);
        //    var bridgeInfo = builder.BuildVersion(NetInfoVersion.Bridge, lateOperations);
        //    var tunnelInfo = builder.BuildVersion(NetInfoVersion.Tunnel, lateOperations);
        //    var slopeInfo = builder.BuildVersion(NetInfoVersion.Slope, lateOperations);

        //    // Setup MenuItems
        //    var swMb = new Stopwatch();
        //    swMb.Start();
        //    if (builder is IMenuItemBuilder)
        //    {
        //        if (groundInfos.Count() > 1)
        //        {
        //            throw new Exception("Multiple netinfo menuitem cannot be build with the IMenuItemBuilder, use the IMenuItemBuildersProvider");
        //        }

        //        var mib = builder as IMenuItemBuilder;
        //        groundInfos[0].SetMenuItemConfig(mib);
        //    }
        //    else if (builder is IMenuItemBuildersProvider)
        //    {
        //        var mibp = builder as IMenuItemBuildersProvider;
        //        var mibs = mibp.MenuItemBuilders.ToDictionary(x => x.Name, StringComparer.InvariantCultureIgnoreCase);

        //        foreach (var mainInfo in groundInfos)
        //        {
        //            if (mibs.ContainsKey(mainInfo.name))
        //            {
        //                var mib = mibs[mainInfo.name];
        //                mainInfo.SetMenuItemConfig(mib);
        //            }
        //        }
        //    }
        //    else
        //    {
        //        throw new Exception("Cannot set the menuitem on netinfo, either implement IMenuItemBuilder or IMenuItemBuildersProvider");
        //    }
        //    swMb.Stop();
        //    Debug.Log($"{builder.Name} menu item built in {swMb.ElapsedMilliseconds}");
        //    // Setup AI
        //    foreach (var mainInfo in groundInfos)
        //    {
        //        var ai = mainInfo.GetComponent<RoadAI>();

        //        ai.m_elevatedInfo = elevatedInfo;
        //        ai.m_bridgeInfo = bridgeInfo;
        //        ai.m_tunnelInfo = tunnelInfo;
        //        ai.m_slopeInfo = slopeInfo;
        //    }

        //    // Returning
        //    foreach (var mainInfo in groundInfos)
        //    {
        //        yield return mainInfo;
        //    }
        //    if (elevatedInfo != null) yield return elevatedInfo;
        //    if (bridgeInfo != null) yield return bridgeInfo;
        //    if (tunnelInfo != null) yield return tunnelInfo;
        //    if (slopeInfo != null) yield return slopeInfo;
        //}

        public static NetInfo BuildVersion(this INetInfoBuilder builder, NetInfoVersion version, ICollection <Action> lateOperations)
        {
            if (builder.SupportedVersions.HasFlag(version))
            {
                var sw = new Stopwatch();
                var basedPrefabName = builder.GetBasedPrefabName(version);
                var builtPrefabName = builder.GetBuiltPrefabName(version);

                sw.Start();
                var info = Prefabs
                           .Find <NetInfo>(basedPrefabName)
                           .Clone(builtPrefabName);
                sw.Stop();
                Debug.Log($"cloned {builder.BasedPrefabName} as {builder.Name} in {sw.ElapsedMilliseconds}");
                builder.BuildUp(info, version);

                var lateBuilder = builder as INetInfoLateBuilder;
                if (lateBuilder != null)
                {
                    var swl = new Stopwatch();
                    swl.Start();
                    lateOperations.Add(() => lateBuilder.LateBuildUp(info, version));
                    swl.Stop();
                    Debug.Log($"cloned {builder.BasedPrefabName} in {swl.ElapsedMilliseconds}");
                }

                return(info);
            }
            else
            {
                return(null);
            }
        }
Example #3
0
        private void LoadModule <M>() where M : IModule, new()
        {
            try {
                var module = new M();
                Debug.Log(string.Format("TFW: Loading module {0}", module.Name));

                module.AssetPath           = AssetPath;
                module.SaveSettingsNeeded += SaveSettings;
                _modules.Add(module);
            } catch (Exception ex) {
                Debug.Log("TFW: Crashed-Module " + typeof(M).Name);
                Debug.Log("TFW: " + ex.Message);
                Debug.Log("TFW: " + ex.ToString());
            }
        }
Example #4
0
        private static Options Load()
        {
            if (RExModule.GetPath() == RExModule.PATH_NOT_FOUND)
            {
                return(new Options());
            }

            Debug.Log(string.Format("REx: Loading config at {0}", FILENAME));

            if (!File.Exists(FILENAME))
            {
                return(new Options());
            }

            try
            {
                var configuration = new Options();
                var xDoc          = new XmlDocument();
                xDoc.Load(FILENAME);

                if (xDoc.DocumentElement == null)
                {
                    return(configuration);
                }

                foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)
                {
                    var nodeValue = true;

                    if (!bool.TryParse(node.InnerText, out nodeValue))
                    {
                        nodeValue = true;
                    }

                    configuration.PartsEnabled[node.Name] = nodeValue;
                }

                return(configuration);
            }
            catch (Exception ex)
            {
                Debug.Log(string.Format("REx: Crashed load config at {0} {1}", FILENAME, ex));
                return(new Options());
            }
        }
Example #5
0
        protected void LoadModulesIfNeeded()
        {
            if (_modulesLoaded)
            {
                return;
            }

            try {
                LoadModule <RExModule>();
                LoadModule <ToolModule>();
            } catch (Exception ex) {
                Debug.Log("TFW: Crashed-Modules");
                Debug.Log("TFW: " + ex.Message);
                Debug.Log("TFW: " + ex.ToString());
            } finally {
                _modulesLoaded = true;
            }
        }
Example #6
0
        private void UpdateInternal()
        {
            if (!_doneWithInstall)
            {
                if (ValidatePrerequisites())
                {
                    Install();
                    Debug.Log(string.Format("TFW: {0} completed", GetType().Name));

                    _doneWithInstall = true;
                }
            }

            if (_doneWithInstall)
            {
                if (InstallationCompleted != null)
                {
                    InstallationCompleted();
                }
            }
        }
Example #7
0
        public static void DisplayLaneProps(this NetInfo info)
        {
            foreach (var lane in info.m_lanes)
            {
                if (lane.m_laneProps != null)
                {
                    Debug.Log(string.Format("TFW: Lane name {0}", lane.m_laneProps.name));

                    if (lane.m_laneProps.m_props != null)
                    {
                        foreach (var prop in lane.m_laneProps.m_props)
                        {
                            if (prop.m_prop != null)
                            {
                                Debug.Log(string.Format("TFW:     Prop name {0}", prop.m_prop.name));
                            }
                        }
                    }
                }
            }
        }
        public static string GetPath(string folderName, ulong workshopId)
        {
            // 1. Check Local path (CurrentUser\Appdata\Local\Colossal Order\Cities_Skylines\Addons\Mods)
            var localPath = Path.Combine(DataLocation.modsPath, folderName);

            Debug.Log(string.Format("REx: Exist={0} DataLocation.modsPath={1}", Directory.Exists(localPath), localPath));

            if (Directory.Exists(localPath))
            {
                return(localPath);
            }

            // 2. Check Steam
            foreach (var mod in Steam.workshop.GetSubscribedItems())
            {
                if (mod.AsUInt64 == workshopId)
                {
                    var workshopPath = Steam.workshop.GetSubscribedItemPath(mod);
                    Debug.Log(string.Format("REx: Exist={0} WorkshopPath={1}", Directory.Exists(workshopPath), workshopPath));
                    if (Directory.Exists(workshopPath))
                    {
                        return(workshopPath);
                    }
                }
            }

            // 3. Check Cities Skylines files folder
            var csFolderPath = Path.Combine(Path.Combine(DataLocation.gameContentPath, "Mods"), folderName);

            Debug.Log(string.Format("REx: Exist={0} DataLocation.gameContentPath={1}", Directory.Exists(csFolderPath), csFolderPath));
            if (Directory.Exists(csFolderPath))
            {
                return(csFolderPath);
            }

            return(PATH_NOT_FOUND);
        }
Example #9
0
        protected void LoadModulesIfNeeded()
        {
            if (_modulesLoaded)
            {
                return;
            }

            try
            {
                var moduleType = typeof(IModule);
                var modType    = this.GetType();
                //// Debug.Log( "Name: " + Name);
                //var him = AppDomain.CurrentDomain.GetAssemblies()
                //                        .SelectMany(a =>
                //                        {
                //                            try
                //                            {
                //                                return a.GetExportedTypes();
                //                            }
                //                            catch (Exception ex)
                //                            {
                //                                Debug.Log("TFW: LoadModulesIfNeeded looking into assembly " + a.FullName);
                //                                Debug.Log("TFW: " + ex.Message);
                //                                Debug.Log("TFW: " + ex.ToString());
                //                                return new Type[] { };
                //                            }
                //                        })
                //    .Where(t => !t.IsAbstract && !t.IsInterface)
                //    .Where(t => moduleType.IsAssignableFrom(t))
                //    .ToList();

                //foreach (var t in him)
                //{
                //    // if (t.Name == "RExATModule")
                //    Debug.Log(Name + "......." + t.Name);
                //}
                _modules = AppDomain.CurrentDomain.GetAssemblies()
                           .SelectMany(a =>
                {
                    try
                    {
                        return(a.GetExportedTypes());
                    }
                    catch (Exception ex)
                    {
                        Debug.Log("TFW: LoadModulesIfNeeded looking into assembly " + a.FullName);
                        Debug.Log("TFW: " + ex.Message);
                        Debug.Log("TFW: " + ex.ToString());
                        return(new Type[] { });
                    }
                })
                           .Where(t => !t.IsAbstract && !t.IsInterface)
                           .Where(t => moduleType.IsAssignableFrom(t))
                           .Where(t => t.GetCustomAttributes(typeof(ModuleAttribute), true)
                                  .OfType <ModuleAttribute>()
                                  .Any(a => a.IsAssociatedWith(modType)))
                           .Select(t =>
                {
                    try
                    {
                        var module = (IModule)Activator.CreateInstance(t);
                        Debug.Log(string.Format("TFW: Loading module {0}", module.Name));

                        module.AssetPath           = AssetPath;
                        module.SaveSettingsNeeded += SaveSettings;
                        return(module);
                    }
                    catch (Exception ex)
                    {
                        Debug.Log("TFW: Crashed-Module " + t.Name);
                        Debug.Log("TFW: " + ex.Message);
                        Debug.Log("TFW: " + ex.ToString());
                        return(null);
                    }
                })
                           .Where(t => t != null)
                           .OrderBy(m => m.Order)
                           .ToArray();
                foreach (var m in _modules)
                {
                    Debug.Log(m.Name);
                }
            }
            catch (Exception ex)
            {
                Debug.Log("TFW: Crashed-Modules");
                Debug.Log("TFW: " + ex.Message);
                Debug.Log("TFW: " + ex.ToString());
            }
            finally
            {
                _modulesLoaded = true;
            }
        }