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)); }
/// <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 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); }
/// <summary> /// loads the preset at 'fullPath' and returns the component /// type described in it /// </summary> public static Type GetFileType(string fullPath) { // 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(fullPath); 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> /// loads the preset at 'fullPath' and returns the component /// type described in it /// </summary> public static Type GetFileType(string fullPath) { // 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(fullPath); 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> /// 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); }
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> /// 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); }