static void ReplaceVehicleAI <TOldAI, TNewAI>(VehicleInfo i) where TOldAI : VehicleAI where TNewAI : VehicleAI { // Requires the object to have the old AI var oldAI = i.gameObject.GetComponent <TOldAI>(); if (oldAI == null || oldAI.GetType() != typeof(TOldAI)) { return; } // Requires the object to not already have the new AI var newAI = i.gameObject.GetComponent <TNewAI>(); if (newAI != null && newAI.GetType() == typeof(TNewAI)) { CODebug.Log(LogChannel.Modding, string.Format("SlowSpeed: {0} already has {1}", i.name, typeof(TNewAI))); return; } CODebug.Log(LogChannel.Modding, string.Format("SlowSpeed: Replacing {0}'s {1} with {2}", i.name, typeof(TOldAI), typeof(TNewAI))); newAI = i.gameObject.AddComponent <TNewAI>(); ShallowCopyTo(oldAI, newAI); oldAI.ReleaseAI(); i.m_vehicleAI = newAI; UObject.Destroy(oldAI); newAI.InitializeAI(); }
public List <ResilientBuildings.ResilientInfoV1> convertVersionZeroListToOne(List <ResilientBuildings.ResilientInfo> list) { CODebug.Log(LogChannel.Modding, Mod.modName + " - Converting save data from version 0 to 1"); List <ResilientBuildings.ResilientInfoV1> newList = new List <ResilientBuildings.ResilientInfoV1>(); for (int i = 0; i < list.Count; i++) { ResilientBuildings.ResilientInfo ri0 = list[i]; ResilientBuildings.ResilientInfoV1 ri1 = new ResilientBuildings.ResilientInfoV1(); ri1.activatedDate = ri0.activatedDate; ri1.buildingID = ri0.buildingID; ri1.chosenLevel = ri0.chosenLevel; ri1.currentVisits = ri0.currentVisits; ri1.description = ri0.description; ri1.goodsBuffer1 = ri0.goodsBuffer1; ri1.goodsBuffer2 = ri0.goodsBuffer2; ri1.goodsBuffer3 = ri0.goodsBuffer3; ri1.goodsBuffer4 = ri0.goodsBuffer4; ri1.resiliencyActivated = ri0.resiliencyActivated; ri1.totalIncome = ri0.totalIncome; ri1.totalVisits = ri0.totalVisits; ri1.unsuscribed = ri0.unsuscribed; ri1.unsuscribeTimer = ri0.unsuscribeTimer; ri1.namesBuffer = new List <string>(); newList.Add(ri1); } return(newList); }
public static void Print(object obj) { if (DEBUG) { CODebug.Log(LogChannel.Modding, "[DistrictStats] " + obj.ToString()); } }
public override void OnSaveData() { base.OnSaveData(); if (!LoadingExtension.installed) { return; } CODebug.Log(LogChannel.Modding, Mod.modName + " - try saving data"); try { if (m_serializedData != null) { BinaryFormatter bFormatter2 = new BinaryFormatter(); MemoryStream mStream2 = new MemoryStream(); bFormatter2.Serialize(mStream2, SAVE_DATA_VERSION); byte[] data2 = mStream2.ToArray(); if (data2 != null) { m_serializedData.SaveData(RESILIENTS_VERSION_ID, data2); } BinaryFormatter bFormatter = new BinaryFormatter(); MemoryStream mStream = new MemoryStream(); bFormatter.Serialize(mStream, s_info.m_resilients); byte[] data = mStream.ToArray(); if (data != null) { m_serializedData.SaveData(RESILIENTS_DATA_ID, data); } BinaryFormatter bFormatter4 = new BinaryFormatter(); MemoryStream mStream4 = new MemoryStream(); bFormatter4.Serialize(mStream4, s_info.m_districts); byte[] data4 = mStream4.ToArray(); if (data4 != null) { m_serializedData.SaveData(RESILIENTS_DISTRICTS_ID, data4); } BinaryFormatter bFormatter3 = new BinaryFormatter(); MemoryStream mStream3 = new MemoryStream(); bFormatter3.Serialize(mStream3, Settings.inst); byte[] data3 = mStream3.ToArray(); if (data3 != null) { m_serializedData.SaveData(RESILIENTS_SETTINGS_ID, data3); } CODebug.Log(LogChannel.Modding, Mod.modName + " - successful saving data"); } else { } } catch (Exception e) { CODebug.Log(LogChannel.Modding, Mod.modName + " - Error saving data " + e.StackTrace); } }
/*********** custom icons *************/ public static UITextureAtlas CreateTextureAtlas(string textureFile, string atlasName, Material baseMaterial, int spriteWidth, int spriteHeight, int spriteCountHor) { Texture2D tex = new Texture2D(spriteWidth * spriteCountHor, spriteHeight, TextureFormat.ARGB32, false); tex.filterMode = FilterMode.Bilinear; try { // LoadTexture System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream textureStream = assembly.GetManifestResourceStream("ResilientOwners." + textureFile); byte[] buf = new byte[textureStream.Length]; //declare arraysize textureStream.Read(buf, 0, buf.Length); // read from stream to byte array tex.LoadImage(buf); tex.Apply(true, true); } catch { CODebug.Log(LogChannel.Modding, "error opening texture file"); } UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); try { // Setup atlas Material material = (Material)Material.Instantiate(baseMaterial); material.mainTexture = tex; atlas.material = material; atlas.name = atlasName; } catch { CODebug.Log(LogChannel.Modding, "error setting texture"); } // Add sprites for (int i = 0; i < spriteCountHor; ++i) { float uw = 1.0f / spriteCountHor; var spriteInfo = new UITextureAtlas.SpriteInfo() { name = atlasName + "_" + i, texture = tex, region = new Rect(i * uw, 0, uw, 1), }; atlas.AddSprite(spriteInfo); } return(atlas); }
static void ShallowCopyTo(object src, object dst) { var srcFields = GetFields(src); var dstFields = GetFields(dst); foreach (var srcField in srcFields) { FieldInfo dstField; if (!dstFields.TryGetValue(srcField.Key, out dstField)) { continue; } CODebug.Log(LogChannel.Modding, string.Format("SlowSpeed: Setting {0} to {1} ({2} to {3})", dstField.Name, srcField.Value.GetValue(src), src.GetType(), dst.GetType())); dstField.SetValue(dst, srcField.Value.GetValue(src)); } }
public override void OnLoadData() { base.OnLoadData(); CODebug.Log(LogChannel.Modding, Mod.modName + " - try loading data"); // if (s_info == null) { // s_info // } try { if (m_serializedData != null) { byte[] data2 = m_serializedData.LoadData(RESILIENTS_VERSION_ID); if (data2 != null) { BinaryFormatter bFormatter = new BinaryFormatter(); MemoryStream mStream = new MemoryStream(data2); s_savedDataVersion = (int)bFormatter.Deserialize(mStream); } else { //save had no data } if (s_savedDataVersion >= 0 && s_savedDataVersion <= SAVE_DATA_VERSION) { byte[] data = m_serializedData.LoadData(RESILIENTS_DATA_ID); if (data != null) { BinaryFormatter bFormatter = new BinaryFormatter(); MemoryStream mStream = new MemoryStream(data); switch (s_savedDataVersion) { case 0: List <ResilientBuildings.ResilientInfo> legacyList = (List <ResilientBuildings.ResilientInfo>)bFormatter.Deserialize(mStream); s_data = convertVersionZeroListToOne(legacyList); break; case 1: case 2: case 3: s_data = (List <ResilientBuildings.ResilientInfoV1>)bFormatter.Deserialize(mStream); break; } CODebug.Log(LogChannel.Modding, Mod.modName + " - successful loading buildings data"); } else { } data = m_serializedData.LoadData(RESILIENTS_DISTRICTS_ID); if (data != null) { BinaryFormatter bFormatter = new BinaryFormatter(); MemoryStream mStream = new MemoryStream(data); switch (s_savedDataVersion) { case 3: s_districts = (List <ResilientBuildings.ResilientDistrict>)bFormatter.Deserialize(mStream); break; } CODebug.Log(LogChannel.Modding, Mod.modName + " - successful loading districts data"); } else { } data = m_serializedData.LoadData(RESILIENTS_SETTINGS_ID); if (data != null) { BinaryFormatter bFormatter = new BinaryFormatter(); MemoryStream mStream = new MemoryStream(data); switch (s_savedDataVersion) { case 2: case 3: Settings.inst = (ResilientSettings)bFormatter.Deserialize(mStream); break; } CODebug.Log(LogChannel.Modding, Mod.modName + " - successful loading settings"); } else { } } else { CODebug.Error(LogChannel.Modding, Mod.modName + " - invalid saved data version"); } } } catch (Exception e) { CODebug.Log(LogChannel.Modding, Mod.modName + " - Error loading data " + e.Message); } }
public override int OnAddResource(EconomyResource resource, int amount, Service service, SubService subService, Level level) { if (!LoadingExtension.installed) { return(amount); } //check if OnAddResource call match what we are looking for if (!resource.Equals(EconomyResource.PrivateIncome)) //interested in private incomes only { return(amount); } if (service != Service.Office) //only office buildings tracked here { return(amount); } //CODebug.Log(LogChannel.Modding, Mod.modName + " - IncomeTracker search building with service " + service + " subservice " + subService + " level " + level + " amount "+amount); SimulationManager instance = Singleton <SimulationManager> .instance; BuildingManager instance2 = Singleton <BuildingManager> .instance; //sync with simulation frame if (checkedFrameIndex != instance.m_currentFrameIndex) { checkedFrameIndex = instance.m_currentFrameIndex; int num6 = (int)(instance.m_currentFrameIndex & 255u); buildingID = (ushort)(num6 * 192); maxBuildingID = (ushort)((num6 + 1) * 192 - 1); } //sync with BuildingManager SimulationStep loop bool skipping = true; bool error = false; do { if ((instance2.m_buildings.m_buffer[buildingID].m_flags & Building.Flags.Created) == Building.Flags.None) { skipping = true; } //abandoned buildings do no go through SimulationStepActive else if ((instance2.m_buildings.m_buffer[buildingID].m_flags & (Building.Flags.Abandoned | Building.Flags.BurnedDown)) != Building.Flags.None) { skipping = true; } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Residential) // { // skipping = false; // if(service != Service.Residential) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Industrial) // { // skipping = false; // if(service != Service.Industrial) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Commercial) // { // skipping = false; // if(service != Service.Commercial) // error = true; // } else if (instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Office) { skipping = false; } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Beautification) // { // skipping = false; // if(service != Service.Beautification) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Citizen) // { // skipping = false; // if(service != Service.Citizen) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Education) // { // skipping = false; // if(service != Service.Education) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Electricity) // { // skipping = false; // if(service != Service.Electricity) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.FireDepartment) // { // skipping = false; // if(service != Service.FireDepartment) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Garbage) // { // skipping = false; // if(service != Service.Garbage) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Government) // { // skipping = false; //// if(service != Service.Governement) //// error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.HealthCare) // { // skipping = false; // if(service != Service.HealthCare) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Monument) // { // skipping = false; // if(service != Service.Monument) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.PoliceDepartment) // { // skipping = false; // if(service != Service.PoliceDepartment) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.PublicTransport) // { // skipping = false; // if(service != Service.PublicTransport) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Road) // { // skipping = false; // if(service != Service.Road) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Water) // { // skipping = false; // if(service != Service.Water) // error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Unused1) // { // skipping = false; //// if(service != Service.) //// error = true; // } // else if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Unused2) // { // skipping = false; //// if(service != Service.Education) //// error = true; // } // else // { //// CODebug.Log(LogChannel.Modding, "income tracker private income for service "+instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service); //// skipping = true; // skipping = false; // if(instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service.ToString() != service.ToString()) // { // CODebug.Log(LogChannel.Modding, "income tracker mismatch "+instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service.ToString()+" "+service.ToString()); // } // } if (skipping) { buildingID++; if (buildingID > maxBuildingID) { error = true; skipping = false; } } }while(skipping); //check if active and history is activated if (!error) { if (instance2.m_buildings.m_buffer[buildingID].Info.m_class.m_service == ItemClass.Service.Office) { int buildIndex = s_info.GetResilientBuildingIndex((ushort)buildingID); String buildinfo2 = Singleton <BuildingManager> .instance.GetBuildingName(buildingID, default(InstanceID)); //CODebug.Log(LogChannel.Modding, Mod.modName + " - IncomeTracker found building with id " + buildingID + " buildIndex " + buildIndex + " " + buildinfo2); if (buildIndex != -1) { ResilientBuildings.ResilientInfoV1 ri = s_info.m_resilients[buildIndex]; ri.totalIncome += amount; s_info.m_resilients[buildIndex] = ri; } } buildingID++; //ensure search starts from next building at next function call } else { CODebug.Log(LogChannel.Modding, Mod.modName + " - IncomeTracker could not meet building with service " + service + " subservice " + subService + " level " + level); } return(amount); }
public static void OutputLog(String logType, String message) { CODebug.Log(LogChannel.Modding, Mod.InternalName + " - " + logType + ": " + message); }