Example #1
0
    /// <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);
            }
#if ANTICHEAT
            else if (fi.FieldType == typeof(ObscuredFloat))
            {
                ObscuredFloat val = ((float)f.Args);
                value = String.Format("{0:0.#######}", val);
            }
            else if (fi.FieldType == typeof(ObscuredVector3))
            {
                ObscuredVector3 val = ((ObscuredVector3)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(ObscuredVector2))
            {
                ObscuredVector2 val = ((ObscuredVector2)f.Args);
                value = String.Format("{0:0.#######}", val.x) + " " +
                        String.Format("{0:0.#######}", val.y);
            }
            else if (fi.FieldType == typeof(ObscuredInt))
            {
                ObscuredInt val = ((ObscuredInt)f.Args);
                value = val.ToString();
            }
            else if (fi.FieldType == typeof(ObscuredBool))
            {
                ObscuredBool val = ((ObscuredBool)f.Args);
                value = val.ToString();
            }
            else if (fi.FieldType == typeof(ObscuredString))
            {
                ObscuredString val = ((ObscuredString)f.Args);
                value = val.ToString();
            }
#endif
            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);
    }