public static GameObjectDTO Map(UnityEngine.GameObject gameObject) { var mappedData = new GameObjectDTO(); SRInfoHelper.Log("Mapping gameobject " + gameObject.name); var goDTO = SRMapper.ReflectionObjectBuilder <DTOs.GameObjectDTO>(gameObject); SRInfoHelper.Log("Mapping " + gameObject.name); List <UnityEngine.Component> components = new List <UnityEngine.Component>(); components.AddRange(gameObject.transform.GetComponents(typeof(UnityEngine.Component))); goDTO.transform.Components = components; goDTO.transform.Children = new List <DTOs.GameObjectDTO>(); foreach (UnityEngine.Transform t in gameObject.transform) { try { goDTO.transform.Children.Add(Map(t.gameObject)); } catch (Exception e) { SRInfoHelper.Log("Exception thrown: " + e.Message + " Inner: " + e.InnerException); } } return(mappedData); }
public static Texture2D LoadTextureFromFile(string fileName) { SRInfoHelper.Log("Loading " + fileName); string filePath = @"C:\Temp" + @"\" + fileName; //string filePath = Manager.GetPluginManager().PluginPath + @"\" + fileName; if (File.Exists(filePath)) { SRInfoHelper.Log("File " + fileName + " exists"); Vector2Int imgSize = ImageHeader.GetDimensions(filePath); SRInfoHelper.Log("Image size " + imgSize.x + " " + imgSize.y); var bytes = File.ReadAllBytes(filePath); SRInfoHelper.Log("Loading bytes into image"); Texture2D tmpTexture = new Texture2D(imgSize.x, imgSize.y); tmpTexture.LoadImage(bytes); SRInfoHelper.Log("Loaded bytes into image"); tmpTexture.name = fileName.Replace(".png", ""); return(tmpTexture); //Texture2D texture = new Texture2D(tmpTexture.width, tmpTexture.height); //SRInfoHelper.Log("Loading bytes into image"); //texture.LoadImage(bytes); //return texture; } else { return(null); } }
public static Image LoadImageFromFile(string fileName) { SRInfoHelper.Log("Loading " + fileName); string filePath = @"C:\Temp" + @"\" + fileName; //string filePath = Manager.GetPluginManager().PluginPath + @"\" + fileName; if (File.Exists(filePath)) { return(Image.FromFile(filePath)); } else { return(null); } }
/// <summary> /// A generic object builder that takes the properties from the reader and puts into a /// new object of the same type as the destination object /// </summary> /// <param name="reader"></param> /// <param name="destination"></param> /// <returns></returns> static public T ReflectionObjectBuilder <T>(Object obj) { //Type sourceType = source.GetType(); Type destinationType = typeof(T); T result = (T)Activator.CreateInstance(destinationType); // columns is a list of attribute names in the reader var sourceFields = obj.GetType().GetFields(); var sourceProps = obj.GetType().GetProperties(); var destProps = destinationType.GetProperties(); var destFields = destinationType.GetFields(); SRInfoHelper.Log("Mapping to " + destinationType + " : " + destProps.Count() + " and mapping from " + obj.GetType() + " " + sourceProps.Count()); // We get properties from the destination type instead of the source type foreach (PropertyInfo pi in destProps) { // set pDest to the currently instanced property type PropertyInfo pDest = destinationType.GetProperty(pi.Name); var pSource = sourceProps.Where(s => s.Name == pi.Name).FirstOrDefault(); var fSource = sourceFields.Where(s => s.Name == pi.Name).FirstOrDefault(); if (pSource == null && fSource == null) { SRInfoHelper.Log("No source found for propety " + pi.Name + "Skipping"); continue; } if (pSource != null) { SRInfoHelper.Log("Property name " + pi.Name + " destination property name " + pSource?.Name); } else { SRInfoHelper.Log("Property name " + pi.Name + " destination field name " + fSource?.Name); } if (pDest != null && pDest.CanWrite) { if (pDest.PropertyType.FullName == "SyndicateMod.DTOs.Sprite") { //SRInfoHelper.Log("Mapping sprite"); DTOs.Sprite destSprite = (DTOs.Sprite)pDest.GetValue(result, null); UnityEngine.Sprite sourceSprite = new UnityEngine.Sprite(); if (pSource != null) { sourceSprite = (UnityEngine.Sprite)pSource.GetValue(obj, null); } else if (fSource != null) { sourceSprite = (UnityEngine.Sprite)fSource.GetValue(obj); } else { continue; } if (sourceSprite == null || sourceSprite.name == null || sourceSprite.name == "") { continue; } SRInfoHelper.Log("Mapping sprite " + sourceSprite.name); DTOs.Sprite mappedSprite = ReflectionObjectBuilder <DTOs.Sprite>(sourceSprite); //SRInfoHelper.Log("Mapped sprite"); if (sourceSprite.texture != null) { FileManager.SaveTextureToFile(sourceSprite.texture); mappedSprite.textureName = sourceSprite.texture.name + ".png"; } if (sourceSprite.associatedAlphaSplitTexture != null) { FileManager.SaveTextureToFile(sourceSprite.associatedAlphaSplitTexture); mappedSprite.associatedAlphaSplitTextureName = sourceSprite.associatedAlphaSplitTexture.name + ".png"; } SRInfoHelper.Log("Saved textures"); pDest.SetValue(result, mappedSprite, null); continue; } bool isCollection = pDest.PropertyType.GetInterfaces() .Any(x => x == typeof(IEnumerable)); if (isCollection && pDest.PropertyType.FullName != "System.String") { Type itemType = pDest.PropertyType; if (itemType.IsArray) { itemType = itemType.GetElementType(); } //if (pSource.FieldType.GetGenericArguments().Any()) // itemType = pSource.FieldType.GetGenericArguments().FirstOrDefault(); SRInfoHelper.Log("Found collection " + pDest.PropertyType + " named " + pDest.Name + " namespace is " + itemType.FullName); if (itemType.FullName.Contains("Syndicate")) { List <DTOs.ModifierData5L> modifiers = new List <DTOs.ModifierData5L>(); if (fSource != null) { foreach (var item in (IEnumerable)fSource.GetValue(obj)) { var mappedItem = ReflectionObjectBuilder <DTOs.ModifierData5L>(item); modifiers.Add(mappedItem); } } else if (pSource != null) { foreach (var item in (IEnumerable)pSource.GetValue(obj, null)) { var mappedItem = ReflectionObjectBuilder <DTOs.ModifierData5L>(item); modifiers.Add(mappedItem); } } result.SetMemberValue(pi.Name, modifiers.ToArray()); } else if (itemType.FullName == "ModifierData5L") { List <ModifierData5L> modifiers = new List <ModifierData5L>(); if (fSource != null) { foreach (var item in (IEnumerable)fSource.GetValue(obj)) { var mappedItem = ReflectionObjectBuilder <ModifierData5L>(item); modifiers.Add(mappedItem); } } else { foreach (var item in (IEnumerable)pSource.GetValue(obj, null)) { var mappedItem = ReflectionObjectBuilder <ModifierData5L>(item); modifiers.Add(mappedItem); } } result.SetMemberValue(pi.Name, modifiers.ToArray()); } else { SRInfoHelper.Log("Processing collection " + pDest.PropertyType); if (fSource != null) { result.SetMemberValue(pi.Name, fSource.GetValue(obj)); } else if (pSource != null) { result.SetMemberValue(pi.Name, pSource.GetValue(obj, null)); } } //result.SetMemberValue(pi.Name, obj.GetMemberValue(pi.Name)); } // Make sure the current property name can be found in the reader else { string value = ""; string type = ""; if (pSource != null) { type = pSource.PropertyType.FullName; value = pSource.GetValue(obj, null).ToString(); SRInfoHelper.Log("Found " + type + " named " + pDest.Name + " with value " + value); result.SetMemberValue(pi.Name, pSource.GetValue(obj, null)); } else if (fSource != null) { type = fSource.FieldType.FullName; value = fSource.GetValue(obj).ToString(); SRInfoHelper.Log("Found " + type + " named " + pDest.Name + " with value " + value); result.SetMemberValue(pi.Name, fSource.GetValue(obj)); } } } } // We get properties from the destination type instead of the source type foreach (FieldInfo pi in destFields) { // set pDest to the currently instanced property type FieldInfo fDest = destinationType.GetField(pi.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); var pSource = sourceProps.Where(s => s.Name == pi.Name).FirstOrDefault(); var fSource = sourceFields.Where(s => s.Name == pi.Name).FirstOrDefault(); if (pSource != null) { SRInfoHelper.Log("Field name " + pi.Name + " destination name " + pSource?.Name); } else { SRInfoHelper.Log("Field name " + pi.Name + " destination name " + fSource?.Name); } if (pSource == null && fSource == null) { SRInfoHelper.Log("No source found for field " + pi.Name + "Skipping"); continue; } if (fDest != null && fDest.IsPublic) { if (fDest.FieldType.FullName == "UnityEngine.Sprite") { //SRInfoHelper.Log("Mapping sprite"); UnityEngine.Sprite destSprite = (UnityEngine.Sprite)fDest.GetValue(result); Sprite sourceSprite = new Sprite(); if (pSource != null) { sourceSprite = (Sprite)pSource.GetValue(obj, null); } else if (fSource != null) { sourceSprite = (Sprite)fSource.GetValue(obj); } else { continue; } SRInfoHelper.Log("Mapping sprite " + sourceSprite); if (sourceSprite == null || string.IsNullOrEmpty(sourceSprite.textureName)) { continue; } UnityEngine.Sprite mappedSprite = ReflectionObjectBuilder <UnityEngine.Sprite>(sourceSprite); SRInfoHelper.Log("Loading textures"); UnityEngine.Texture2D texture = null; UnityEngine.Texture2D associatedAlphaSplitTexture = null; if (!string.IsNullOrEmpty(sourceSprite.textureName)) { texture = FileManager.LoadTextureFromFile(sourceSprite.textureName); SRInfoHelper.Log("Loaded texture " + texture.name + " size " + texture.height + "x" + texture.width); } if (!string.IsNullOrEmpty(sourceSprite.associatedAlphaSplitTextureName)) { associatedAlphaSplitTexture = FileManager.LoadTextureFromFile(sourceSprite.associatedAlphaSplitTextureName); //mappedSprite.associatedAlphaSplitTexture.Resize(texture.width, texture.height); SRInfoHelper.Log("Loaded alpha split texture " + associatedAlphaSplitTexture.name + " size " + associatedAlphaSplitTexture.height + "x" + associatedAlphaSplitTexture.width); //mappedSprite.associatedAlphaSplitTexture.SetPixels(associatedAlphaSplitTexture.GetPixels()); } //SRInfoHelper.Log("Creating Texture " + mappedSprite.rect + " " +mappedSprite.pivot); var createdSprite = UnityEngine.Sprite.Create(texture, new UnityEngine.Rect(0, 0, texture.width, texture.height), new UnityEngine.Vector2(16, 16), 100); //createdSprite.textureRect.position = mappedSprite.textureRect.position; SRInfoHelper.Log("Loaded textures"); fDest.SetValue(result, createdSprite); continue; } bool isCollection = fDest.FieldType.GetInterfaces() .Any(x => x == typeof(IEnumerable)); if (isCollection && fDest.FieldType.FullName != "System.String") { Type itemType = fDest.FieldType; if (itemType.IsArray) { itemType = itemType.GetElementType(); } //if (pSource.FieldType.GetGenericArguments().Any()) // itemType = pSource.FieldType.GetGenericArguments().FirstOrDefault(); SRInfoHelper.Log("Found collection " + fDest.FieldType + " named " + fDest.Name + " namespace is " + itemType.FullName); if (itemType.FullName.Contains("Syndicate")) { List <DTOs.ModifierData5L> modifiers = new List <DTOs.ModifierData5L>(); if (fSource != null) { foreach (var item in (IEnumerable)fSource.GetValue(obj)) { var mappedItem = ReflectionObjectBuilder <DTOs.ModifierData5L>(item); modifiers.Add(mappedItem); } } else { foreach (var item in (IEnumerable)pSource.GetValue(obj, null)) { var mappedItem = ReflectionObjectBuilder <DTOs.ModifierData5L>(item); modifiers.Add(mappedItem); } } fDest.SetValue(result, modifiers.ToArray()); } else if (itemType.FullName == "ModifierData5L") { List <ModifierData5L> modifiers = new List <ModifierData5L>(); if (fSource != null) { foreach (var item in (IEnumerable)fSource.GetValue(obj)) { var mappedItem = ReflectionObjectBuilder <ModifierData5L>(item); modifiers.Add(mappedItem); } } else { foreach (var item in (IEnumerable)pSource.GetValue(obj, null)) { var mappedItem = ReflectionObjectBuilder <ModifierData5L>(item); modifiers.Add(mappedItem); } } fDest.SetValue(result, modifiers.ToArray()); //result.SetMemberValue(pi.Name, modifiers.ToArray()); } else { if (pSource != null) { fDest.SetValue(result, pSource.GetValue(obj, null)); } else if (fSource != null) { fDest.SetValue(result, fSource.GetValue(obj)); } } //result.SetMemberValue(pi.Name, obj.GetMemberValue(pi.Name)); } // Make sure the current property name can be found in the reader else { string value; string type; if (pSource != null) { type = pSource.PropertyType.FullName; value = pSource.GetValue(obj, null).ToString(); SRInfoHelper.Log("Found type of " + type + " named " + fDest.Name + " with value " + value); fDest.SetValue(result, pSource.GetValue(obj, null)); } else if (fSource != null) { type = fSource.FieldType.FullName; value = fSource.GetValue(obj).ToString(); SRInfoHelper.Log("Found " + type + " named " + fDest.Name + " with value " + value); result.SetMemberValue(pi.Name, fSource.GetValue(obj)); } } } } return(result); }
public static ItemManager.ItemData CopyItem(int itemNumber) { var itemManager = Manager.Get().m_ItemManager_template.GetComponent <ItemManager>(); if (Manager.GetItemManager() != null || Manager.GetItemManager().GetAllItems() != null || Manager.GetItemManager().GetAllItems().Count > 60) { itemManager = Manager.GetItemManager(); SRInfoHelper.Log($"Adding item copy of item {itemNumber} to m_ItemManager"); } else { SRInfoHelper.Log($"Adding item copy of item {itemNumber} to m_ItemManager_template"); } ItemManager.ItemData sourceItem = itemManager.GetAllItems().Where(i => i.m_ID == itemNumber).First(); ItemManager.ItemData newItem = new ItemManager.ItemData(); int newitemNumber = itemManager.GetAllItems().Max(i => i.m_ID) + 1; newItem.m_ID = newitemNumber; //energyGeneratorV2.m_AbilityIDs.Add(); //energyGeneratorV2.m_LocName = "Energy Generator V3"; try { newItem.m_AbilityIDs = sourceItem.m_AbilityIDs.ToList(); } catch { } try { newItem.m_AbilityMasks = sourceItem.m_AbilityMasks.ToList(); } catch { } try { newItem.m_BlueprintCost = sourceItem.m_BlueprintCost; } catch { } try { newItem.m_Cost = sourceItem.m_Cost; } catch { } try { newItem.m_GearSubCategory = sourceItem.m_GearSubCategory; } catch { } try { newItem.m_MinResearchersRequired = sourceItem.m_MinResearchersRequired; } catch { } try { newItem.m_Modifiers = sourceItem.m_Modifiers.ToArray(); } catch { } try { newItem.m_PrereqID = sourceItem.m_PrereqID; } catch { } try { newItem.m_Slot = sourceItem.m_Slot; } catch { } try { newItem.m_UIIcon = sourceItem.m_UIIcon; } catch { } try { newItem.m_BlueprintProgressionValue = sourceItem.m_BlueprintProgressionValue; } catch { } try { newItem.m_CurrentResearchCost = sourceItem.m_CurrentResearchCost; } catch { } try { newItem.m_Expanded = sourceItem.m_Expanded; } catch { } try { newItem.m_FindBlueprintCost = sourceItem.m_FindBlueprintCost; } catch { } try { newItem.m_FindPrototypeCost = sourceItem.m_FindPrototypeCost; } catch { } try { newItem.m_OverrideAmmo = sourceItem.m_OverrideAmmo; } catch { } try { newItem.m_PlayerCanResearchFromStart = sourceItem.m_PlayerCanResearchFromStart; } catch { } try { newItem.m_Progression = sourceItem.m_Progression; } catch { } try { newItem.m_PrototypeCost = sourceItem.m_PrototypeCost; } catch { } try { newItem.m_PrototypeIsInTheWorld = sourceItem.m_PrototypeIsInTheWorld; } catch { } try { newItem.m_PrototypeProgressionValue = sourceItem.m_PrototypeProgressionValue; } catch { } try { newItem.m_PrototypeRandomReleaseStage = sourceItem.m_PrototypeRandomReleaseStage; } catch { } try { newItem.m_ResearchCost = sourceItem.m_ResearchCost; } catch { } try { newItem.m_ResearchDataPoints = sourceItem.m_ResearchDataPoints; } catch { } try { newItem.m_StealthVsCombat = sourceItem.m_StealthVsCombat; } catch { } try { newItem.m_ValidWeaponAugmentationWeaponMask = sourceItem.m_ValidWeaponAugmentationWeaponMask; } catch { } try { newItem.m_WeaponType = sourceItem.m_WeaponType; } catch { } try { } catch { } var langLookup = TextManager.Get().GetFieldValue <Dictionary <string, TextManager.LocElement> >("m_FastLanguageLookup"); var name = langLookup.Where(l => l.Key == $"ITEM_{itemNumber}_NAME").First().Value; var company = langLookup.Where(l => l.Key == $"ITEM_{itemNumber}_COMPANY").First().Value; var desc = langLookup.Where(l => l.Key == $"ITEM_{itemNumber}_DESCRIPTION").First().Value; TextManager.LocElement newItemName = new TextManager.LocElement(); newItemName.m_token = $"ITEM_{newitemNumber}_NAME"; newItemName.m_Translations = name.m_Translations.ToArray(); //newItemName.m_Translations[2] = itemName; langLookup.Add(newItemName.m_token, newItemName); try { TextManager.LocElement newItemCompany = new TextManager.LocElement(); newItemCompany.m_token = $"ITEM_{newitemNumber}_COMPANY"; newItemCompany.m_Translations = company.m_Translations.ToArray(); langLookup.Add(newItemCompany.m_token, newItemCompany); } catch { } newItem.m_PlayerHasPrototype = true; newItem.m_PlayerHasBlueprints = true; newItem.m_Count = 5; TextManager.LocElement newItemDescription = new TextManager.LocElement(); newItemDescription.m_token = $"ITEM_{newitemNumber}_DESCRIPTION"; newItemDescription.m_Translations = desc.m_Translations.ToArray(); //newItemDescription.m_Translations[2] = @"Grenade V2 now with even more bang."; langLookup.Add(newItemDescription.m_token, newItemDescription); Manager.GetItemManager().m_ItemDefinitions.Add(newItem); return(newItem); }