/// <summary> /// opens a dialog for saving presets /// </summary> static private void ShowSaveDifferenceDialog(vp_Component component) { string path = Application.dataPath; // LORE: in order not to overwrite existing values in a disk // preset, we'll load the disk preset before saving over it. // since the file dialog system will execute its callback // twice in case of an already existing file (and delete the // target file in the process) we need to store the preset // in memory outside of the callback and skip loading it on // the second iteration vp_ComponentPreset diskPreset = null; vp_FileDialog.Create(vp_FileDialog.Mode.Save, "Save Tweaks", path, delegate(string filename) { // only attempt to load the disk preset the first time // the callback is executed if (diskPreset == null) { diskPreset = new vp_ComponentPreset(); // attempt to load target preset into memory, ignoring // load errors in the process bool logErrorState = vp_ComponentPreset.LogErrors; vp_ComponentPreset.LogErrors = false; diskPreset.LoadTextStream(filename); vp_ComponentPreset.LogErrors = logErrorState; } vp_FileDialog.Result = vp_ComponentPreset.SaveDifference(component.InitialState.Preset, component, filename, diskPreset); }, ".txt"); }
public static string Save(Component component, string fullPath) { vp_ComponentPreset vp_ComponentPreset = new vp_ComponentPreset(); vp_ComponentPreset.InitFromComponent(component); return(vp_ComponentPreset.Save(vp_ComponentPreset, fullPath, false)); }
/// <summary> /// saves every supported field of 'preset' to text at 'fullPath' /// </summary> public static string Save(Component component, string fullPath) { vp_ComponentPreset preset = new vp_ComponentPreset(); preset.InitFromComponent(component); return(Save(preset, fullPath)); }
/// <summary> /// creates an empty preset in memory, copies a component's /// type and values into it and returns the preset /// </summary> public static vp_ComponentPreset CreateFromComponent(Component component) { vp_ComponentPreset preset = new vp_ComponentPreset(); preset.m_ComponentType = component.GetType(); foreach (FieldInfo f in component.GetType().GetFields()) { if (f.IsPublic) { if (f.FieldType == typeof(float) || f.FieldType == typeof(Vector4) || f.FieldType == typeof(Vector3) || f.FieldType == typeof(Vector2) || f.FieldType == typeof(int) || f.FieldType == typeof(bool) || f.FieldType == typeof(string)) { preset.m_Fields.Add(new Field(f.FieldHandle, f.GetValue(component))); } } } return(preset); }
public static bool Load(Component component, string fullPath) { vp_ComponentPreset vp_ComponentPreset = new vp_ComponentPreset(); vp_ComponentPreset.LoadTextStream(fullPath); return(vp_ComponentPreset.Apply(component, vp_ComponentPreset)); }
public static vp_ComponentPreset LoadFromResources(Component component, string resourcePath) { vp_ComponentPreset vp_ComponentPreset = new vp_ComponentPreset(); vp_ComponentPreset.LoadFromResources(resourcePath); vp_ComponentPreset.Apply(component, vp_ComponentPreset); return(vp_ComponentPreset); }
public static vp_ComponentPreset LoadFromTextAsset(Component component, TextAsset file) { vp_ComponentPreset vp_ComponentPreset = new vp_ComponentPreset(); vp_ComponentPreset.LoadFromTextAsset(file); vp_ComponentPreset.Apply(component, vp_ComponentPreset); return(vp_ComponentPreset); }
/// <summary> /// helper method to load a preset from the resources folder, /// and refresh settings /// </summary> public vp_ComponentPreset Load(string path) { vp_ComponentPreset preset = vp_ComponentPreset.LoadFromResources(this, path); RefreshDefaultState(); Refresh(); return(preset); }
/// <summary> /// helper method to load a preset from a text asset, /// and refresh settings /// </summary> public vp_ComponentPreset Load(TextAsset asset) { vp_ComponentPreset preset = vp_ComponentPreset.LoadFromTextAsset(this, asset); RefreshDefaultState(); Refresh(); return(preset); }
/// <summary> /// static overload: creates and loads a preset and sets all /// the values on 'component', then returns the preset /// </summary> public static vp_ComponentPreset LoadFromTextAsset(vp_Component component, TextAsset file) { vp_ComponentPreset preset = new vp_ComponentPreset(); preset.LoadFromTextAsset(file); Apply(component, preset); return(preset); }
public static bool Apply(Component component, vp_ComponentPreset preset) { if (preset == null) { vp_ComponentPreset.Error("Tried to apply a preset that was null in '" + vp_Utility.GetErrorLocation(1) + "'"); return(false); } if (preset.m_ComponentType == null) { vp_ComponentPreset.Error("Preset ComponentType was null in '" + vp_Utility.GetErrorLocation(1) + "'"); return(false); } if (component == null) { vp_ComponentPreset.Error("Component was null when attempting to apply preset in '" + vp_Utility.GetErrorLocation(1) + "'"); return(false); } if (component.GetType() != preset.m_ComponentType) { string text = "a '" + preset.m_ComponentType + "' preset"; if (preset.m_ComponentType == null) { text = "an unknown preset type"; } vp_ComponentPreset.Error(string.Concat(new string[] { "Tried to apply ", text, " to a '", component.GetType().ToString(), "' component in '", vp_Utility.GetErrorLocation(1), "'" })); return(false); } foreach (vp_ComponentPreset.Field current in preset.m_Fields) { FieldInfo[] fields = component.GetType().GetFields(); for (int i = 0; i < fields.Length; i++) { FieldInfo fieldInfo = fields[i]; if (fieldInfo.FieldHandle == current.FieldHandle) { fieldInfo.SetValue(component, current.Args); } } } return(true); }
public static string SaveDifference(vp_ComponentPreset initialStatePreset, Component modifiedComponent, string fullPath, vp_ComponentPreset diskPreset) { if (initialStatePreset.ComponentType != modifiedComponent.GetType()) { vp_ComponentPreset.Error("Tried to save difference between different type components in 'SaveDifference'"); return(null); } vp_ComponentPreset vp_ComponentPreset = new vp_ComponentPreset(); vp_ComponentPreset.InitFromComponent(modifiedComponent); vp_ComponentPreset vp_ComponentPreset2 = new vp_ComponentPreset(); vp_ComponentPreset2.m_ComponentType = vp_ComponentPreset.ComponentType; for (int i = 0; i < vp_ComponentPreset.m_Fields.Count; i++) { if (!initialStatePreset.m_Fields[i].Args.Equals(vp_ComponentPreset.m_Fields[i].Args)) { vp_ComponentPreset2.m_Fields.Add(vp_ComponentPreset.m_Fields[i]); } } foreach (vp_ComponentPreset.Field current in diskPreset.m_Fields) { bool flag = true; foreach (vp_ComponentPreset.Field current2 in vp_ComponentPreset2.m_Fields) { if (current.FieldHandle == current2.FieldHandle) { flag = false; } } bool flag2 = false; foreach (vp_ComponentPreset.Field current3 in vp_ComponentPreset.m_Fields) { if (current.FieldHandle == current3.FieldHandle) { flag2 = true; } } if (!flag2) { flag = false; } if (flag) { vp_ComponentPreset2.m_Fields.Add(current); } } return(vp_ComponentPreset.Save(vp_ComponentPreset2, fullPath, true)); }
public static Type GetFileType(string fullPath) { bool logErrors = vp_ComponentPreset.LogErrors; vp_ComponentPreset.LogErrors = false; vp_ComponentPreset vp_ComponentPreset = new vp_ComponentPreset(); vp_ComponentPreset.LoadTextStream(fullPath); vp_ComponentPreset.LogErrors = logErrors; if (vp_ComponentPreset != null && vp_ComponentPreset.m_ComponentType != null) { return(vp_ComponentPreset.m_ComponentType); } return(null); }
public static Type GetFileTypeFromAsset(TextAsset asset) { bool logErrors = vp_ComponentPreset.LogErrors; vp_ComponentPreset.LogErrors = false; vp_ComponentPreset vp_ComponentPreset = new vp_ComponentPreset(); vp_ComponentPreset.LoadFromTextAsset(asset); vp_ComponentPreset.LogErrors = logErrors; if (vp_ComponentPreset != null && vp_ComponentPreset.m_ComponentType != null) { return(vp_ComponentPreset.m_ComponentType); } return(null); }
public static vp_ComponentPreset CreateFromComponent(Component component) { vp_ComponentPreset vp_ComponentPreset = new vp_ComponentPreset(); vp_ComponentPreset.m_ComponentType = component.GetType(); FieldInfo[] fields = component.GetType().GetFields(); for (int i = 0; i < fields.Length; i++) { FieldInfo fieldInfo = fields[i]; if (fieldInfo.IsPublic && (fieldInfo.FieldType == typeof(float) || fieldInfo.FieldType == typeof(Vector4) || fieldInfo.FieldType == typeof(Vector3) || fieldInfo.FieldType == typeof(Vector2) || fieldInfo.FieldType == typeof(int) || fieldInfo.FieldType == typeof(bool) || fieldInfo.FieldType == typeof(string))) { vp_ComponentPreset.m_Fields.Add(new vp_ComponentPreset.Field(fieldInfo.FieldHandle, fieldInfo.GetValue(component))); } } return(vp_ComponentPreset); }
/// <summary> /// this method applies a preset onto the passed component, /// returning true on success /// </summary> public static bool Apply(Component component, vp_ComponentPreset preset) { if (preset == null) { Error("Tried to apply a preset that was null in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } if (preset.m_ComponentType == null) { Error("Preset ComponentType was null in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } if (component == null) { Error("Component was null when attempting to apply preset in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } if (component.GetType() != preset.m_ComponentType) { string type = "a '" + preset.m_ComponentType + "' preset"; if (preset.m_ComponentType == null) { type = "an unknown preset type"; } Error("Tried to apply " + type + " to a '" + component.GetType().ToString() + "' component in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } // component and preset both seem ok, so set the preset fields // onto the component foreach (Field f in preset.m_Fields) { foreach (FieldInfo destField in component.GetType().GetFields()) { if (destField.FieldHandle == f.FieldHandle) { destField.SetValue(component, f.Args); } } } return(true); }
/////////////////////////////////////////////////////////// // this method applies a preset onto the passed component, // returning true on success /////////////////////////////////////////////////////////// public static bool Apply(Component component, vp_ComponentPreset preset) { if (preset == null) { Error("Tried to apply a preset that was null in '" + GetErrorLocation() + "'"); return false; } if (preset.m_ComponentType == null) { Error("Preset ComponentType was null in '" + GetErrorLocation() + "'"); return false; } if (component == null) { Error("Component was null when attempting to apply preset in '" + GetErrorLocation() + "'"); return false; } if (component.GetType() != preset.m_ComponentType) { string type = "a '" + preset.m_ComponentType + "' preset"; if (preset.m_ComponentType == null) type = "an unknown preset type"; Error("Tried to apply " + type + " to a '" + component.GetType().ToString() + "' component in '" + GetErrorLocation() + "'"); return false; } // component and preset both seem ok, so set the preset fields // onto the component foreach (Field f in preset.m_Fields) { foreach (FieldInfo destField in component.GetType().GetFields()) { if (destField.FieldHandle == f.FieldHandle) destField.SetValue(component, f.Args); } } return true; }
/// <summary> /// this method applies a preset onto the passed component, /// returning true on success /// </summary> public static bool Apply(vp_Component component, vp_ComponentPreset preset) { if (preset == null) { Error("Tried to apply a preset that was null in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } if (preset.m_ComponentType == null) { Error("Preset ComponentType was null in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } if (component == null) { UnityEngine.Debug.LogWarning("Warning: Component was null when attempting to apply preset in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } if (component.Type != preset.m_ComponentType) { string type = "a '" + preset.m_ComponentType + "' preset"; if (preset.m_ComponentType == null) { type = "an unknown preset type"; } Error("Applied " + type + " to a '" + component.Type.ToString() + "' component in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } // component and preset both seem ok, so set the preset fields // onto the component for (int p = 0; p < preset.m_Fields.Count; p++) { FieldInfo destField = FieldInfo.GetFieldFromHandle(preset.m_Fields[p].FieldHandle); destField.SetValue(component, preset.m_Fields[p].Args); } return(true); }
/// <summary> /// loads the preset at 'fullPath' and returns the component /// type described in it /// </summary> public static Type GetFileTypeFromAsset(TextAsset asset) { // attempt to load target preset into memory, ignoring // load errors in the process bool logErrorState = LogErrors; LogErrors = false; vp_ComponentPreset preset = new vp_ComponentPreset(); preset.LoadFromTextAsset(asset); LogErrors = logErrorState; // try to get hold of a preset and a component type from the file if (preset != null) { if (preset.m_ComponentType != null) { return(preset.m_ComponentType); } } // the file was not found return(null); }
/// <summary> /// creates an empty preset in memory, copies a component's /// type and values into it and returns the preset /// </summary> public static vp_ComponentPreset CreateFromComponent(vp_Component component) { // TODO: used? vp_ComponentPreset preset = new vp_ComponentPreset(); preset.m_ComponentType = component.Type; foreach (FieldInfo f in preset.m_ComponentType.GetFields()) { if (f.IsPublic) { if (f.FieldType == typeof(float) || f.FieldType == typeof(Vector4) || f.FieldType == typeof(Vector3) || f.FieldType == typeof(Vector2) || f.FieldType == typeof(int) || f.FieldType == typeof(bool) || f.FieldType == typeof(string) #if ANTICHEAT || f.FieldType == typeof(ObscuredFloat) || f.FieldType == typeof(ObscuredVector3) || f.FieldType == typeof(ObscuredVector2) || f.FieldType == typeof(ObscuredInt) || f.FieldType == typeof(ObscuredBool) || f.FieldType == typeof(ObscuredString) #endif ) { preset.m_Fields.Add(new Field(f.FieldHandle, f.GetValue(component))); } } } return(preset); }
/// <summary> /// /// </summary> public static void GenerateStatesAndPresetsFromDerivedComponent(Component derivedComponent, Component baseComponent, string path) { //System.Type derivedType = derivedComponent.GetType(); // TEST (see below) System.Type baseType = baseComponent.GetType(); // TEST: disabled to allow converting from vp_FPController to // vp_CapsuleController. evaluate this down the line //if (!vp_EditorUtility.IsSameOrSubclass(baseType, derivedType)) // return; vp_Component vpDerived = derivedComponent as vp_Component; vp_Component vpBase = baseComponent as vp_Component; if (vpDerived == null) return; if (vpBase == null) return; for (int v = 0; v < vpDerived.States.Count; v++) { // abort if old state has no text asset vp_State oldState = vpDerived.States[v]; if (oldState.TextAsset == null) continue; // abort if we fail to load old text asset into a preset vp_ComponentPreset preset = new vp_ComponentPreset(); if (!preset.LoadFromTextAsset(oldState.TextAsset)) continue; // try to make the preset compatible with the base component. this // will fail if it has no compatible fields, in which case we abort if (preset.TryMakeCompatibleWithComponent(vpBase) < 1) continue; // we have a new preset that is compatible with the base component. // save it at a temporary, auto-generated path string typeName = oldState.TypeName.Replace("vp_FP", ""); typeName = typeName.Replace("vp_", ""); string filePath = path + "/" + typeName + "_" + vpBase.gameObject.name + "_" + oldState.Name + ".txt"; vp_ComponentPreset.Save(preset, filePath); AssetDatabase.Refresh(); // add a corresponding state, into which we load the new preset vp_State newState = new vp_State(baseType.Name, vpDerived.States[v].Name, null, null); vpBase.States.Add(newState); // System.Threading.Thread.Sleep(100); // might come in handy on slow disk (?) newState.TextAsset = AssetDatabase.LoadAssetAtPath(filePath, typeof(TextAsset)) as TextAsset; } }
/// <summary> /// helper method to apply a preset from memory and refresh /// settings /// </summary> public void ApplyPreset(vp_ComponentPreset preset) { vp_ComponentPreset.Apply(this, preset); RefreshDefaultState(); Refresh(); }
/// <summary> /// helper method to apply a preset from memory and refresh /// settings /// </summary> public void ApplyPreset(vp_ComponentPreset preset) { vp_ComponentPreset.Apply(this, preset); RefreshDefaultState(); Refresh(); }
/// <summary> /// saves only the fields that were changed /// </summary> public static string SaveDifference(vp_ComponentPreset initialStatePreset, Component modifiedComponent, string fullPath, vp_ComponentPreset diskPreset) { if (initialStatePreset.ComponentType != modifiedComponent.GetType()) { Error("Tried to save difference between different type components in 'SaveDifference'"); return(null); } // create a preset to hold the current state of the component vp_ComponentPreset modifiedPreset = new vp_ComponentPreset(); modifiedPreset.InitFromComponent(modifiedComponent); // create an empty preset of the same type vp_ComponentPreset result = new vp_ComponentPreset(); result.m_ComponentType = modifiedPreset.ComponentType; for (int v = 0; v < modifiedPreset.m_Fields.Count; v++) { // if the field in the current preset has been changed since // the initial preset was created, add the differing field to // our result if (!initialStatePreset.m_Fields[v].Args.Equals(modifiedPreset.m_Fields[v].Args)) { result.m_Fields.Add(modifiedPreset.m_Fields[v]); } } // if the target filename already contains a preset with values, // we copy those values into our new result - but only if they // don't exist in the new result // for each field in the disk preset foreach (Field d in diskPreset.m_Fields) { bool copyField = true; // check it against all the fields in our new preset foreach (Field r in result.m_Fields) { // if the field also exists in the 'result' preset, // don't copy it since it has changed if (d.FieldHandle == r.FieldHandle) { copyField = false; } } // only copy the field if it in fact belongs in this type of // component (we may be saving over a different preset type // and should not be copying unknown parameters) bool found = false; foreach (Field m in modifiedPreset.m_Fields) { if (d.FieldHandle == m.FieldHandle) { found = true; } } if (found == false) { copyField = false; } // done, copy the field if (copyField) { result.m_Fields.Add(d); } } return(Save(result, fullPath, true)); }
/// <summary> /// loads the preset at 'fullPath' and returns the component /// type described in it /// </summary> public static Type GetFileTypeFromAsset(TextAsset asset) { // attempt to load target preset into memory, ignoring // load errors in the process bool logErrorState = LogErrors; LogErrors = false; vp_ComponentPreset preset = new vp_ComponentPreset(); preset.LoadFromTextAsset(asset); LogErrors = logErrorState; // try to get hold of a preset and a component type from the file if (preset != null) { if (preset.m_ComponentType != null) return preset.m_ComponentType; } // the file was not found return null; }
/// <summary> /// saves every supported field of 'preset' to text at 'fullPath' /// </summary> public static string Save(vp_ComponentPreset savePreset, string fullPath, bool isDifference = false) { m_FullPath = fullPath; // if the targeted file already exists, we take a look // at it to see if it has the same type as 'component' // attempt to load target preset into memory, ignoring // load errors in the process bool logErrorState = LogErrors; LogErrors = false; vp_ComponentPreset preset = new vp_ComponentPreset(); preset.LoadTextStream(m_FullPath); LogErrors = logErrorState; // if we got hold of a preset and a component type from // the file, confirm overwrite if (preset != null) { if (preset.m_ComponentType != null) { // warn user if the type is not same as the passed 'component' if (preset.ComponentType != savePreset.ComponentType) { return("'" + ExtractFilenameFromPath(m_FullPath) + "' has the WRONG component type: " + preset.ComponentType.ToString() + ".\n\nDo you want to replace it with a " + savePreset.ComponentType.ToString() + "?"); } // confirm that the user does in fact want to overwrite this file if (System.IO.File.Exists(m_FullPath)) { if (isDifference) { return("This will update '" + ExtractFilenameFromPath(m_FullPath) + "' with only the values modified since pressing Play or setting a state.\n\nContinue?"); } else { return("'" + ExtractFilenameFromPath(m_FullPath) + "' already exists.\n\nDo you want to replace it?"); } } } // if we end up here there was a file but it didn't make sense, so confirm overwrite if (System.IO.File.Exists(m_FullPath)) { return("'" + ExtractFilenameFromPath(m_FullPath) + "' has an UNKNOWN component type.\n\nDo you want to replace it?"); } } // go ahead and save 'component' to the text file ClearTextFile(); Append("///////////////////////////////////////////////////////////"); Append("// Component Preset Script"); Append("///////////////////////////////////////////////////////////\n"); // append component type Append("ComponentType " + savePreset.ComponentType.Name); // scan component for all its fields. NOTE: any types // to be supported must be included here. string prefix; string value; foreach (Field f in savePreset.m_Fields) { prefix = ""; value = ""; FieldInfo fi = FieldInfo.GetFieldFromHandle(f.FieldHandle); if (fi.FieldType == typeof(float)) { value = String.Format("{0:0.#######}", ((float)f.Args)); } else if (fi.FieldType == typeof(Vector4)) { Vector4 val = ((Vector4)f.Args); value = String.Format("{0:0.#######}", val.x) + " " + String.Format("{0:0.#######}", val.y) + " " + String.Format("{0:0.#######}", val.z) + " " + String.Format("{0:0.#######}", val.w); } else if (fi.FieldType == typeof(Vector3)) { Vector3 val = ((Vector3)f.Args); value = String.Format("{0:0.#######}", val.x) + " " + String.Format("{0:0.#######}", val.y) + " " + String.Format("{0:0.#######}", val.z); } else if (fi.FieldType == typeof(Vector2)) { Vector2 val = ((Vector2)f.Args); value = String.Format("{0:0.#######}", val.x) + " " + String.Format("{0:0.#######}", val.y); } else if (fi.FieldType == typeof(int)) { value = ((int)f.Args).ToString(); } else if (fi.FieldType == typeof(bool)) { value = ((bool)f.Args).ToString(); } else if (fi.FieldType == typeof(string)) { value = ((string)f.Args); } else { prefix = "//"; value = "<NOTE: Type '" + fi.FieldType.Name.ToString() + "' can't be saved to preset.>"; } // print field name and value to the text file if (!string.IsNullOrEmpty(value) && fi.Name != "Persist") { Append(prefix + fi.Name + " " + value); } } return(null); }
/// <summary> /// saves every supported field of 'preset' to text at 'fullPath' /// </summary> public static string Save(Component component, string fullPath) { vp_ComponentPreset preset = new vp_ComponentPreset(); preset.InitFromComponent(component); return Save(preset, fullPath); }
public static string Save(vp_ComponentPreset savePreset, string fullPath, bool isDifference = false) { vp_ComponentPreset.m_FullPath = fullPath; bool logErrors = vp_ComponentPreset.LogErrors; vp_ComponentPreset.LogErrors = false; vp_ComponentPreset vp_ComponentPreset = new vp_ComponentPreset(); vp_ComponentPreset.LoadTextStream(vp_ComponentPreset.m_FullPath); vp_ComponentPreset.LogErrors = logErrors; if (vp_ComponentPreset != null) { if (vp_ComponentPreset.m_ComponentType != null) { if (vp_ComponentPreset.ComponentType != savePreset.ComponentType) { return(string.Concat(new string[] { "'", vp_ComponentPreset.ExtractFilenameFromPath(vp_ComponentPreset.m_FullPath), "' has the WRONG component type: ", vp_ComponentPreset.ComponentType.ToString(), ".\n\nDo you want to replace it with a ", savePreset.ComponentType.ToString(), "?" })); } if (File.Exists(vp_ComponentPreset.m_FullPath)) { if (isDifference) { return("This will update '" + vp_ComponentPreset.ExtractFilenameFromPath(vp_ComponentPreset.m_FullPath) + "' with only the values modified since pressing Play or setting a state.\n\nContinue?"); } return("'" + vp_ComponentPreset.ExtractFilenameFromPath(vp_ComponentPreset.m_FullPath) + "' already exists.\n\nDo you want to replace it?"); } } if (File.Exists(vp_ComponentPreset.m_FullPath)) { return("'" + vp_ComponentPreset.ExtractFilenameFromPath(vp_ComponentPreset.m_FullPath) + "' has an UNKNOWN component type.\n\nDo you want to replace it?"); } } vp_ComponentPreset.ClearTextFile(); vp_ComponentPreset.Append("///////////////////////////////////////////////////////////"); vp_ComponentPreset.Append("// Component Preset Script"); vp_ComponentPreset.Append("///////////////////////////////////////////////////////////\n"); vp_ComponentPreset.Append("ComponentType " + savePreset.ComponentType.Name); foreach (vp_ComponentPreset.Field current in savePreset.m_Fields) { string str = string.Empty; string text = string.Empty; FieldInfo fieldFromHandle = FieldInfo.GetFieldFromHandle(current.FieldHandle); if (fieldFromHandle.FieldType == typeof(float)) { text = string.Format("{0:0.#######}", (float)current.Args); } else if (fieldFromHandle.FieldType == typeof(Vector4)) { Vector4 vector = (Vector4)current.Args; text = string.Concat(new string[] { string.Format("{0:0.#######}", vector.x), " ", string.Format("{0:0.#######}", vector.y), " ", string.Format("{0:0.#######}", vector.z), " ", string.Format("{0:0.#######}", vector.w) }); } else if (fieldFromHandle.FieldType == typeof(Vector3)) { Vector3 vector2 = (Vector3)current.Args; text = string.Concat(new string[] { string.Format("{0:0.#######}", vector2.x), " ", string.Format("{0:0.#######}", vector2.y), " ", string.Format("{0:0.#######}", vector2.z) }); } else if (fieldFromHandle.FieldType == typeof(Vector2)) { Vector2 vector3 = (Vector2)current.Args; text = string.Format("{0:0.#######}", vector3.x) + " " + string.Format("{0:0.#######}", vector3.y); } else if (fieldFromHandle.FieldType == typeof(int)) { text = ((int)current.Args).ToString(); } else if (fieldFromHandle.FieldType == typeof(bool)) { text = ((bool)current.Args).ToString(); } else if (fieldFromHandle.FieldType == typeof(string)) { text = (string)current.Args; } else { str = "//"; text = "<NOTE: Type '" + fieldFromHandle.FieldType.Name.ToString() + "' can't be saved to preset.>"; } if (!string.IsNullOrEmpty(text) && fieldFromHandle.Name != "Persist") { vp_ComponentPreset.Append(str + fieldFromHandle.Name + " " + text); } } return(null); }
/// <summary> /// static overload: creates and loads a preset and sets all /// the values on 'component', then returns the preset /// </summary> public static vp_ComponentPreset LoadFromResources(vp_Component component, string resourcePath) { vp_ComponentPreset preset = new vp_ComponentPreset(); preset.LoadFromResources(resourcePath); Apply(component, preset); return preset; }
/// <summary> /// static overload: creates and loads a preset and sets all /// the values on 'component', then returns the preset /// </summary> public static vp_ComponentPreset LoadFromTextAsset(vp_Component component, TextAsset file) { vp_ComponentPreset preset = new vp_ComponentPreset(); preset.LoadFromTextAsset(file); Apply(component, preset); return preset; }
/// <summary> /// static overload: creates and loads a preset and sets all /// the values on 'component' /// </summary> public static bool Load(vp_Component component, string fullPath) { vp_ComponentPreset preset = new vp_ComponentPreset(); preset.LoadTextStream(fullPath); return Apply(component, preset); }
/// <summary> /// creates an empty preset in memory, copies a component's /// type and values into it and returns the preset /// </summary> public static vp_ComponentPreset CreateFromComponent(Component component) { vp_ComponentPreset preset = new vp_ComponentPreset(); preset.m_ComponentType = component.GetType(); foreach (FieldInfo f in preset.m_ComponentType.GetFields()) { if (f.IsPublic) { if (f.FieldType == typeof(float) || f.FieldType == typeof(Vector4) || f.FieldType == typeof(Vector3) || f.FieldType == typeof(Vector2) || f.FieldType == typeof(int) || f.FieldType == typeof(bool) || f.FieldType == typeof(string)) { preset.m_Fields.Add(new Field(f.FieldHandle, f.GetValue(component))); } } } return preset; }
/// <summary> /// saves only the fields that were changed /// </summary> public static string SaveDifference(vp_ComponentPreset initialStatePreset, Component modifiedComponent, string fullPath, vp_ComponentPreset diskPreset) { if (initialStatePreset.ComponentType != modifiedComponent.GetType()) { Error("Tried to save difference between different type components in 'SaveDifference'"); return null; } // create a preset to hold the current state of the component vp_ComponentPreset modifiedPreset = new vp_ComponentPreset(); modifiedPreset.InitFromComponent(modifiedComponent); // create an empty preset of the same type vp_ComponentPreset result = new vp_ComponentPreset(); result.m_ComponentType = modifiedPreset.ComponentType; for (int v = 0; v < modifiedPreset.m_Fields.Count; v++) { // if the field in the current preset has been changed since // the initial preset was created, add the differing field to // our result if (!initialStatePreset.m_Fields[v].Args.Equals(modifiedPreset.m_Fields[v].Args)) result.m_Fields.Add(modifiedPreset.m_Fields[v]); } // if the target filename already contains a preset with values, // we copy those values into our new result - but only if they // don't exist in the new result // for each field in the disk preset foreach (Field d in diskPreset.m_Fields) { bool copyField = true; // check it against all the fields in our new preset foreach (Field r in result.m_Fields) { // if the field also exists in the 'result' preset, // don't copy it since it has changed if (d.FieldHandle == r.FieldHandle) copyField = false; } // only copy the field if it in fact belongs in this type of // component (we may be saving over a different preset type // and should not be copying unknown parameters) bool found = false; foreach (Field m in modifiedPreset.m_Fields) { if (d.FieldHandle == m.FieldHandle) found = true; } if (found == false) copyField = false; // done, copy the field if (copyField) result.m_Fields.Add(d); } return Save(result, fullPath, true); }
/// <summary> /// /// </summary> public static void GenerateStatesAndPresetsFromDerivedComponent(Component derivedComponent, Component baseComponent, string path) { //System.Type derivedType = derivedComponent.GetType(); // TEST (see below) System.Type baseType = baseComponent.GetType(); // TEST: disabled to allow converting from vp_FPController to // vp_CapsuleController. evaluate this down the line //if (!vp_EditorUtility.IsSameOrSubclass(baseType, derivedType)) // return; vp_Component vpDerived = derivedComponent as vp_Component; vp_Component vpBase = baseComponent as vp_Component; if (vpDerived == null) { return; } if (vpBase == null) { return; } for (int v = 0; v < vpDerived.States.Count; v++) { // abort if old state has no text asset vp_State oldState = vpDerived.States[v]; if (oldState.TextAsset == null) { continue; } // abort if we fail to load old text asset into a preset vp_ComponentPreset preset = new vp_ComponentPreset(); if (!preset.LoadFromTextAsset(oldState.TextAsset)) { continue; } // try to make the preset compatible with the base component. this // will fail if it has no compatible fields, in which case we abort if (preset.TryMakeCompatibleWithComponent(vpBase) < 1) { continue; } // we have a new preset that is compatible with the base component. // save it at a temporary, auto-generated path string typeName = oldState.TypeName.Replace("vp_FP", ""); typeName = typeName.Replace("vp_", ""); string filePath = path + "/" + typeName + "_" + vpBase.gameObject.name + "_" + oldState.Name + ".txt"; vp_ComponentPreset.Save(preset, filePath); AssetDatabase.Refresh(); // add a corresponding state, into which we load the new preset vp_State newState = new vp_State(baseType.Name, vpDerived.States[v].Name, null, null); vpBase.States.Add(newState); // System.Threading.Thread.Sleep(100); // might come in handy on slow disk (?) newState.TextAsset = AssetDatabase.LoadAssetAtPath(filePath, typeof(TextAsset)) as TextAsset; } }
/// <summary> /// saves every supported field of 'preset' to text at 'fullPath' /// </summary> public static string Save(vp_ComponentPreset savePreset, string fullPath, bool isDifference = false) { m_FullPath = fullPath; // if the targeted file already exists, we take a look // at it to see if it has the same type as 'component' // attempt to load target preset into memory, ignoring // load errors in the process bool logErrorState = LogErrors; LogErrors = false; vp_ComponentPreset preset = new vp_ComponentPreset(); preset.LoadTextStream(m_FullPath); LogErrors = logErrorState; // if we got hold of a preset and a component type from // the file, confirm overwrite if (preset != null) { if (preset.m_ComponentType != null) { // warn user if the type is not same as the passed 'component' if (preset.ComponentType != savePreset.ComponentType) return ("'" + ExtractFilenameFromPath(m_FullPath) + "' has the WRONG component type: " + preset.ComponentType.ToString() + ".\n\nDo you want to replace it with a " + savePreset.ComponentType.ToString() + "?"); // confirm that the user does in fact want to overwrite this file if (System.IO.File.Exists(m_FullPath)) { if (isDifference) return ("This will update '" + ExtractFilenameFromPath(m_FullPath) + "' with only the values modified since pressing Play or setting a state.\n\nContinue?"); else return ("'" + ExtractFilenameFromPath(m_FullPath) + "' already exists.\n\nDo you want to replace it?"); } } // if we end up here there was a file but it didn't make sense, so confirm overwrite if (System.IO.File.Exists(m_FullPath)) return ("'" + ExtractFilenameFromPath(m_FullPath) + "' has an UNKNOWN component type.\n\nDo you want to replace it?"); } // go ahead and save 'component' to the text file ClearTextFile(); Append("///////////////////////////////////////////////////////////"); Append("// Component Preset Script"); Append("///////////////////////////////////////////////////////////\n"); // append component type Append("ComponentType " + savePreset.ComponentType.Name); // scan component for all its fields. NOTE: any types // to be supported must be included here. string prefix; string value; foreach (Field f in savePreset.m_Fields) { prefix = ""; value = ""; FieldInfo fi = FieldInfo.GetFieldFromHandle(f.FieldHandle); if (fi.FieldType == typeof(float)) value = String.Format("{0:0.#######}", ((float)f.Args)); else if (fi.FieldType == typeof(Vector4)) { Vector4 val = ((Vector4)f.Args); value = String.Format("{0:0.#######}", val.x) + " " + String.Format("{0:0.#######}", val.y) + " " + String.Format("{0:0.#######}", val.z) + " " + String.Format("{0:0.#######}", val.w); } else if (fi.FieldType == typeof(Vector3)) { Vector3 val = ((Vector3)f.Args); value = String.Format("{0:0.#######}", val.x) + " " + String.Format("{0:0.#######}", val.y) + " " + String.Format("{0:0.#######}", val.z); } else if (fi.FieldType == typeof(Vector2)) { Vector2 val = ((Vector2)f.Args); value = String.Format("{0:0.#######}", val.x) + " " + String.Format("{0:0.#######}", val.y); } else if (fi.FieldType == typeof(int)) value = ((int)f.Args).ToString(); else if (fi.FieldType == typeof(bool)) value = ((bool)f.Args).ToString(); else if (fi.FieldType == typeof(string)) value = ((string)f.Args); else { prefix = "//"; value = "<NOTE: Type '" + fi.FieldType.Name.ToString() + "' can't be saved to preset.>"; } // print field name and value to the text file if (!string.IsNullOrEmpty(value) && fi.Name != "Persist") Append(prefix + fi.Name + " " + value); } return null; }
/// <summary> /// opens a dialog for saving presets /// </summary> static private void ShowSaveDifferenceDialog(vp_Component component) { string path = Application.dataPath; // LORE: in order not to overwrite existing values in a disk // preset, we'll load the disk preset before saving over it. // since the file dialog system will execute its callback // twice in case of an already existing file (and delete the // target file in the process) we need to store the preset // in memory outside of the callback and skip loading it on // the second iteration vp_ComponentPreset diskPreset = null; vp_FileDialog.Create(vp_FileDialog.Mode.Save, "Save Tweaks", path, delegate(string filename) { // only attempt to load the disk preset the first time // the callback is executed if (diskPreset == null) { diskPreset = new vp_ComponentPreset(); // attempt to load target preset into memory, ignoring // load errors in the process bool logErrorState = vp_ComponentPreset.LogErrors; vp_ComponentPreset.LogErrors = false; diskPreset.LoadTextStream(filename); vp_ComponentPreset.LogErrors = logErrorState; } vp_FileDialog.Result = vp_ComponentPreset.SaveDifference(component.InitialState.Preset, component, filename, diskPreset); }, ".txt"); }
/// <summary> /// this method applies a preset onto the passed component, /// returning true on success /// </summary> public static bool Apply(vp_Component component, vp_ComponentPreset preset) { if (preset == null) { Error("Tried to apply a preset that was null in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } if (preset.m_ComponentType == null) { Error("Preset ComponentType was null in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } if (component == null) { UnityEngine.Debug.LogWarning("Warning: Component was null when attempting to apply preset in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } if (component.Type != preset.m_ComponentType) { string type = "a '" + preset.m_ComponentType + "' preset"; if (preset.m_ComponentType == null) { type = "an unknown preset type"; } Error("Applied " + type + " to a '" + component.Type.ToString() + "' component in '" + vp_Utility.GetErrorLocation() + "'"); return(false); } // component and preset both seem ok, so set the preset fields // onto the component for (int p = 0; p < preset.m_Fields.Count; p++) { FieldInfo destField = FieldInfo.GetFieldFromHandle(preset.m_Fields[p].FieldHandle); #if ANTICHEAT if ((destField.FieldType == typeof(ObscuredFloat)) && (preset.m_Fields[p].Args.GetType() == typeof(float))) { ObscuredFloat o = (float)preset.m_Fields[p].Args; destField.SetValue(component, o); } else if ((destField.FieldType == typeof(ObscuredVector3)) && (preset.m_Fields[p].Args.GetType() == typeof(Vector3))) { ObscuredVector3 o = (Vector3)preset.m_Fields[p].Args; destField.SetValue(component, o); } else if ((destField.FieldType == typeof(ObscuredVector2)) && (preset.m_Fields[p].Args.GetType() == typeof(Vector2))) { ObscuredVector2 o = (Vector2)preset.m_Fields[p].Args; destField.SetValue(component, o); } else if ((destField.FieldType == typeof(ObscuredInt)) && (preset.m_Fields[p].Args.GetType() == typeof(int))) { ObscuredInt o = (int)preset.m_Fields[p].Args; destField.SetValue(component, o); } else if ((destField.FieldType == typeof(ObscuredBool)) && (preset.m_Fields[p].Args.GetType() == typeof(bool))) { ObscuredBool o = (bool)preset.m_Fields[p].Args; destField.SetValue(component, o); } else if ((destField.FieldType == typeof(ObscuredString)) && (preset.m_Fields[p].Args.GetType() == typeof(string))) { ObscuredString o = (string)preset.m_Fields[p].Args; destField.SetValue(component, o); } else #endif destField.SetValue(component, preset.m_Fields[p].Args); } return(true); }