Exemplo n.º 1
0
        private DuplexMessage BuildMessage()
        {
            var filterType = MessageFilterType.Checksum | MessageFilterType.Compression;

            if (SecurityEnabled)
            {
                filterType |= MessageFilterType.Crypto;
            }

            if (Parameter == null)
            {
                serializeMode = SerializeMode.None;
            }
            else
            {
                if (BasicType.IsBasicType(Parameter.GetType()))
                {
                    serializeMode = SerializeMode.BasicType;
                }
            }

            return(DuplexMessage
                   .CreateCommandMessage(
                       ID,
                       Version,
                       CommandCode,
                       filterType,
                       DEFAULT_FILTER_CODE,
                       serializeMode,
                       Parameter));
        }
Exemplo n.º 2
0
 public void SetProperty(SerializeMode mode, params Expression <Func <T, object> >[] plist)
 {
     foreach (var exp in plist)
     {
         SetProperty(GetNameFrom(exp.Body), mode);
     }
 }
Exemplo n.º 3
0
 public JsonWriter(TextWriter writer, SerializeMode options = SerializeMode.Serialize, bool pretty = false, int level = 0)
 {
     this._out = writer;
     this._options = options;
     this._pretty = pretty;
     this._initiallevel = level;
     this._currentlevel = level;
     scope= new Scope();
 }
Exemplo n.º 4
0
 public JsonWriter(TextWriter writer, SerializeMode options = SerializeMode.Serialize, bool pretty = false, int level = 0)
 {
     this._out          = writer;
     this._options      = options;
     this._pretty       = pretty;
     this._initiallevel = level;
     this._currentlevel = level;
     scope = new Scope();
 }
Exemplo n.º 5
0
 /// <summary>
 /// Serialize an object and store it in a file.
 /// </summary>
 /// <param name="mode">The instance of an object to serialize</param>
 /// <param name="instance">The instance of an object to serialize.</param>
 /// <param name="filePath">The location on disk.</param>
 /// <returns>A serialized MemoryStream.</returns>
 public static void Serialize(SerializeMode mode, object instance, string filePath)
 {
     using (FileStream fs = new FileStream(filePath, FileMode.Create))
     {
         MemoryStream stream = Serialize(mode, instance);
         byte[]       buffer = stream.ToArray();
         fs.Write(buffer, 0, buffer.Length);
         fs.Flush();
     }
 }
Exemplo n.º 6
0
 public SerializeMode this[string name]
 {
     get {
         SerializeMode n = SerializeMode.None;
         if (Rules.TryGetValue(name, out n))
         {
             return(n);
         }
         return(SerializeMode.None);
     }
 }
Exemplo n.º 7
0
        public static string ToPropertyTypeValue(this SerializeMode mode)
        {
            switch (mode)
            {
            case SerializeMode.LargeObject:
                return(SerializeConstant.PropertyTypeLargeValue);

            case SerializeMode.ShortObject:
            default:
                return(SerializeConstant.PropertyTypeShortValue);
            }
        }
Exemplo n.º 8
0
        public void Set <T>(Expression <Func <T, object> > exp, SerializeMode mode)
        {
            MemberExpression body;
            UnaryExpression  u = exp.Body as UnaryExpression;

            if (u != null)
            {
                body = u.Operand as MemberExpression;
            }
            else
            {
                body = exp.Body as MemberExpression;
            }
            Set <T>(body.Member.Name, mode);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Deserialize an object from a location on disk or network.
        /// </summary>
        /// <param name="mode">The mode to deserialize the object.</param>
        /// <param name="instance">Specify an instance of an object to deserialize.</param>
        /// <param name="filePath">The path to the serialized object.</param>
        /// <returns>A deserialized object.</returns>
        public static T Deserialize <T>(SerializeMode mode, T instance, string filePath)
        {
            byte[] buffer = null;

            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    return(Deserialize <T>(mode, instance, buffer));
                }
            }
            finally
            {
                buffer = null;
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Deserialize an object from a byte array
        /// </summary>
        /// <param name="mode">The mode to deserialize the object.</param>
        /// <param name="instance">Specify an instance of an object to deserialize.</param>
        /// <param name="buffer">A byte array containing the serialized object, that needs
        /// to be deserialized</param>
        /// <returns>A deserialized object.</returns>
        public static T Deserialize <T>(SerializeMode mode, T instance, byte[] buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("The byte array is null.");
            }

            lock (_Lock)
            {
                switch (mode)
                {
                case SerializeMode.Xml:
                    return(DeserializeXml <T>(instance, buffer));

                default:
                    return(DeserializeBinary <T>(instance, buffer));
                }
            }
        }
Exemplo n.º 11
0
 public static DuplexMessage CreateCommandMessage(
     string messageId,
     MessageVersion version,
     string identifier,
     MessageFilterType filterType,
     byte[] filterCode,
     MessageState state,
     ErrorCode errorCode,
     SerializeMode serializeMode,
     CommandCode commandCode,
     object content = null)
 {
     if (content != null)
     {
         return(new DuplexMessage(new MessageHeader(
                                      messageId,
                                      version,
                                      identifier,
                                      filterCode,
                                      filterType,
                                      state,
                                      errorCode,
                                      serializeMode,
                                      commandCode,
                                      MessageType.Command), content));
     }
     else
     {
         return(new DuplexMessage(new MessageHeader(
                                      messageId,
                                      version,
                                      identifier,
                                      MessageFilterFactory.CreateDefaultFilterCode(),
                                      MessageFilterType.Checksum,
                                      state,
                                      errorCode,
                                      SerializeMode.None,
                                      commandCode,
                                      MessageType.Command), null));
     }
 }
Exemplo n.º 12
0
        public bool Serialize(string filePath, SerializeMode serializeMode)
        {
            string inputFileType = Path.GetExtension(filePath);
            bool   success       = true;

            switch (inputFileType)
            {
            case ".obj": { SerializeOBJ(filePath, serializeMode); break; }

            case ".emap": { SerializeEMAP(filePath, serializeMode); break; }

            default:
            {
                Logger.LogMessage(Logger.LogType.Warning, "Unable to read output filetype. Please select an .obj or .emap file");
                success = false;
                break;
            }
            }

            return(success);
        }
Exemplo n.º 13
0
        public override void Serialize(string path, IEnumerable <KeyValuePair <string, string> > dic)
        {
            var resDic = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            if (SerializeMode.HasFlag(SerializeModeEnum.Append) && File.Exists(path))
            {
                var previousItems = new ResXResourceReader(path).ToDictionary();

                foreach (var kvp in previousItems)
                {
                    resDic.Add(kvp.Key, kvp.Value);
                }
            }

            foreach (var kvp in dic)
            {
                if (resDic.ContainsKey(kvp.Key))
                {
                    if (SerializeMode.HasFlag(SerializeModeEnum.OverwriteOldWithNew))
                    {
                        resDic[kvp.Key] = kvp.Value;
                    }
                }
                else
                {
                    if (SerializeMode.HasFlag(SerializeModeEnum.AddNonExisting))
                    {
                        resDic.Add(kvp.Key, kvp.Value);
                    }
                }
            }

            using (var rWriter = new ResXResourceWriter(path))
            {
                foreach (var kvp in resDic)
                {
                    rWriter.AddResource(kvp.Key, kvp.Value);
                }
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Serializes any instances of any objects.
        /// </summary>
        /// <param name="mode">The instance of an object to serialize</param>
        /// <param name="instance">The instance of an object to serialize.</param>
        /// <returns>A serialized MemoryStream.</returns>
        public static MemoryStream Serialize(SerializeMode mode, object instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("The instance object is null.");
            }

            lock (_Lock)
            {
                MemoryStream stream = new MemoryStream();
                switch (mode)
                {
                case SerializeMode.Xml:
                    SerializeXml(ref stream, ref instance);
                    return(stream);

                default:
                    SerializeBinary(ref stream, ref instance);
                    return(stream);
                }
            }
        }
Exemplo n.º 15
0
        public static DuplexMessage CreateCommandMessage(
            string messageId,
            MessageVersion version,
            CommandCode commandCode,
            MessageFilterType filterType,
            byte[] filterCode,
            SerializeMode serializeMode,
            object content)
        {
            var identifier = ObjectHost.Host.Resolve <IIdentifierProvider>().GetIdentifier();

            if (content != null)
            {
                return(CreateCommandMessage(
                           messageId,
                           version,
                           identifier,
                           filterType,
                           filterCode,
                           MessageState.Success,
                           ErrorCode.NoError,
                           serializeMode,
                           commandCode,
                           content));
            }
            else
            {
                return(CreateCommandMessage(
                           messageId,
                           version,
                           identifier,
                           MessageFilterType.Checksum,
                           MessageFilterFactory.CreateDefaultFilterCode(),
                           MessageState.Success,
                           ErrorCode.NoError,
                           SerializeMode.None,
                           commandCode));
            }
        }
Exemplo n.º 16
0
 public MessageHeader(
     string messageID,
     MessageVersion version,
     string identifier,
     byte[] filterCode,
     MessageFilterType filterType,
     MessageState state,
     ErrorCode errorCode,
     SerializeMode serializeMode,
     CommandCode commandCode,
     MessageType messageType)
 {
     this.MessageID = messageID;
     this.Version = version;
     this.State = state;
     this.FilterCode = filterCode;
     this.FilterType = filterType;
     this.ErrorCode = errorCode;
     this.SerializeMode = serializeMode;
     this.CommandCode = commandCode;
     this.Identifier = identifier;
     this.MessageType = messageType;
 }
Exemplo n.º 17
0
 public MessageHeader(
     string messageID,
     MessageVersion version,
     string identifier,
     byte[] filterCode,
     MessageFilterType filterType,
     MessageState state,
     ErrorCode errorCode,
     SerializeMode serializeMode,
     CommandCode commandCode,
     MessageType messageType)
 {
     this.MessageID     = messageID;
     this.Version       = version;
     this.State         = state;
     this.FilterCode    = filterCode;
     this.FilterType    = filterType;
     this.ErrorCode     = errorCode;
     this.SerializeMode = serializeMode;
     this.CommandCode   = commandCode;
     this.Identifier    = identifier;
     this.MessageType   = messageType;
 }
        private void ConvertButton_Click(object sender, RoutedEventArgs e)
        {
            //Filepath checks
            string inputFilePath  = InputFilePathTextbox.Text;
            string outputFilePath = OutputFilePathTextbox.Text;

            if (File.Exists(inputFilePath) == false)
            {
                Logger.LogMessage(Logger.LogType.Warning, "Input filepath doesn't exist!");
                return;
            }

            if (outputFilePath == string.Empty)
            {
                Logger.LogMessage(Logger.LogType.Warning, "No output filepath selected!");
                return;
            }

            //--------------------
            // Deserialize
            //--------------------
            Model model = new Model();

            try
            {
                Logger.LogMessage(Logger.LogType.Normal, "Reading input file...");
                bool success = model.Deserialize(inputFilePath);

                if (success == false)
                {
                    Logger.LogMessage(Logger.LogType.Error, "Error parsing input file");
                    return;
                }
            }
            catch (Exception exception)
            {
                Logger.LogMessage(Logger.LogType.Error, "Unhandled input error: " + exception.Message);
                return;
            }

            //--------------------
            // Serialize EMAP
            //--------------------
            try
            {
                Logger.LogMessage(Logger.LogType.Normal, "Writing to output file...");

                SerializeMode serializeMode = SerializeMode.Append;
                if (OverwriteRadioButton.IsChecked == true)
                {
                    serializeMode = SerializeMode.Overwrite;
                }

                bool success = model.Serialize(outputFilePath, serializeMode);

                if (success == false)
                {
                    Logger.LogMessage(Logger.LogType.Error, "Error writing output file");
                    return;
                }
            }
            catch (Exception exception)
            {
                Logger.LogMessage(Logger.LogType.Error, "Unhandled output error: " + exception.Message);
                return;
            }

            //-------------------------
            // Copy thumbnail images
            //-------------------------
            Logger.LogMessage(Logger.LogType.Normal, "Copying thumbnail images...");

            //Check if we can find the "DefaultImage". If so, copy it to the same folder with the same name (.jpg)
            string fileName = Path.GetFileNameWithoutExtension(outputFilePath);
            string filePath = outputFilePath.Substring(0, outputFilePath.Length - Path.GetFileName(outputFilePath).Length);

            if (File.Exists(s_DefaultImagePath))
            {
                string newPath = filePath + fileName + ".jpg";

                if (File.Exists(newPath) == false)
                {
                    File.Copy(s_DefaultImagePath, newPath);
                }
            }

            //Same process for "DefaultImage_thumb"
            if (File.Exists(s_DefaultImageThumbPath))
            {
                string newPath = filePath + fileName + "_thumb.jpg";

                if (File.Exists(newPath) == false)
                {
                    File.Copy(s_DefaultImageThumbPath, newPath);
                }
            }

            //Done!
            Logger.LogMessage(Logger.LogType.Normal, "Conversion success!");
        }
Exemplo n.º 19
0
 public static SpanJsonSerializer <ExcludeNullsOriginalCaseResolver <byte> > Create(SerializeMode mode)
 {
     return(new SpanJsonSerializer <ExcludeNullsOriginalCaseResolver <byte> >(mode));
 }
Exemplo n.º 20
0
 public void SetMode(string propertyName, SerializeMode mode)
 {
     Rules[propertyName] = mode;
 }
Exemplo n.º 21
0
        private void SerializeEMAP(string filePath, SerializeMode serializeMode)
        {
            StringBuilder brushStringBuilder = new StringBuilder();

            //Convert
            int seed = 0;

            for (int i = 0; i < m_ModelObjects.Count; ++i)
            {
                brushStringBuilder.Append(m_ModelObjects[i].SerializeEMAP(ref seed));
            }

            string brushString = brushStringBuilder.ToString();
            string fileString  = string.Empty;

            //Write
            bool isNewFile = !File.Exists(filePath);

            //---------------
            // Existing file
            //---------------
            if (isNewFile == false)
            {
                //Find out where we should start writing
                StreamReader streamReader = new StreamReader(filePath);
                string       fileContent  = streamReader.ReadToEnd();
                streamReader.Close();

                int breaklineLength = 0;
                if (fileContent.Contains("\r\n"))
                {
                    breaklineLength = 2;
                }
                else if (fileContent.Contains("\n"))
                {
                    breaklineLength = 1;
                }
                else
                {
                    Logger.LogMessage(Logger.LogType.Error, "File is using unconventional line endings!");
                    return;
                }

                //Very basic check to see if it's a Prodeus file? Easily bypassed of course, but why would you?
                //Note: We really assume here that the file has been constructed by the game itself. If not this will all fail horribly.
                if (fileContent.StartsWith("Version_1") == false)
                {
                    Logger.LogMessage(Logger.LogType.Error, "Couldn't write to existing output file, this file was most likely not saved by Prodeus itself.");
                    return;
                }

                //Determine end point of brushes (Brushes are always above Nodes when Prodeus exports the file)
                int nodeStartIndex = fileContent.IndexOf("Nodes{");
                if (nodeStartIndex < 0)
                {
                    Logger.LogMessage(Logger.LogType.Error, "Couldn't write to existing output file, this file was most likely not saved by Prodeus itself.");
                    return;
                }

                int brushesEndIndex = nodeStartIndex - breaklineLength; //breakline

                string preNewBrushString  = string.Empty;
                string postNewBrushString = fileContent.Substring(brushesEndIndex - 1, fileContent.Length - brushesEndIndex);

                if (serializeMode == SerializeMode.Overwrite)
                {
                    //Determine start point
                    string brushHeader     = "Brushes{";
                    int    brushStartIndex = fileContent.IndexOf(brushHeader);
                    brushStartIndex += brushHeader.Length;

                    preNewBrushString = fileContent.Substring(0, brushStartIndex);

                    //Bit of a cheaty newline
                    brushString = "\n" + brushString;
                }

                else if (serializeMode == SerializeMode.Append)
                {
                    //Determine start point
                    preNewBrushString = fileContent.Substring(0, brushesEndIndex - 1);
                }

                //Recombine with the new brushes in the middle
                StringBuilder fileStringBuilder = new StringBuilder();
                fileStringBuilder.Append(preNewBrushString);
                fileStringBuilder.Append(brushString);
                fileStringBuilder.Append(postNewBrushString);

                fileString = fileStringBuilder.ToString();
            }

            //---------------
            // New file
            //---------------
            else
            {
                StringBuilder fileStringBuilder = new StringBuilder();

                //EMAP Version number
                fileStringBuilder.AppendLine("Version_1");

                //Default map properties
                fileStringBuilder.AppendLine("MapProperties{");
                fileStringBuilder.AppendLine("mapID=");
                fileStringBuilder.AppendLine("mapTitle=" + m_Name);
                fileStringBuilder.AppendLine("mapDescription=Converted from " + m_OriginalFilePath);
                fileStringBuilder.AppendLine("mapTags=");
                fileStringBuilder.AppendLine("isCampaign=False");
                fileStringBuilder.AppendLine("mapRunes=");
                fileStringBuilder.AppendLine("mapWeapons=");
                fileStringBuilder.AppendLine("mapEnemyCount=0");
                fileStringBuilder.AppendLine("music=Hotspot");
                fileStringBuilder.AppendLine("lavaColor=0.15,0.1,0.05,1");
                fileStringBuilder.AppendLine("lavaEmissiveColor=1,0.35,0.15,1");
                fileStringBuilder.AppendLine("waterColor=0,0.05,0.15,1");
                fileStringBuilder.AppendLine("waterEmissiveColor=0,0,0,1");
                fileStringBuilder.AppendLine("wasteColor=0.05,0.1,0.03,1");
                fileStringBuilder.AppendLine("wasteEmissiveColor=0.2,1,0.1,1");
                fileStringBuilder.AppendLine("}");

                //No layers
                fileStringBuilder.AppendLine("Layers{");
                fileStringBuilder.AppendLine("}");

                //Default color
                fileStringBuilder.AppendLine("Colors{");
                fileStringBuilder.AppendLine("Default=1,1,1,1");
                fileStringBuilder.AppendLine("}");

                //Default material
                fileStringBuilder.AppendLine("Materials{");
                fileStringBuilder.AppendLine("Blockout");
                fileStringBuilder.AppendLine("}");

                //The actual model
                fileStringBuilder.AppendLine("Brushes{");
                fileStringBuilder.Append(brushString);
                fileStringBuilder.AppendLine("}");

                //No nodes
                fileStringBuilder.AppendLine("Nodes{");
                fileStringBuilder.AppendLine("}");

                fileString = fileStringBuilder.ToString();
            }

            //Actually Write it to a file
            StreamWriter streamWriter = new StreamWriter(filePath);

            streamWriter.Write(fileString);
            streamWriter.Close();
        }
Exemplo n.º 22
0
        private void SerializeOBJ(string filePath, SerializeMode serializeMode)
        {
            string fileString = string.Empty;

            //Write
            bool isNewFile = !File.Exists(filePath);

            //---------------
            // Existing file
            //---------------
            if (isNewFile == true || serializeMode == SerializeMode.Overwrite)
            {
                string brushString = SerializeOBJBrushes(0, 0, 0);

                StringBuilder fileStringBuilder = new StringBuilder();
                fileStringBuilder.AppendLine("# Converted from " + m_OriginalFilePath);
                fileStringBuilder.Append(brushString);

                fileString = fileStringBuilder.ToString();
            }
            else if (isNewFile == false && serializeMode == SerializeMode.Append)
            {
                //Open the existing file and count the amount of vertices, texture coordinates & normals
                int vertexIndexOffset            = 0;
                int textureCoordinateIndexOffset = 0;
                int normalIndexOffset            = 0;

                //Open the file
                StreamReader  streamReader      = new StreamReader(filePath);
                StringBuilder fileStringBuilder = new StringBuilder();

                //Read the file line by line
                string currentLine = streamReader.ReadLine(); //works for \n and \rn I assume
                while (currentLine != null)
                {
                    if (currentLine.StartsWith("v "))
                    {
                        vertexIndexOffset += 1;
                    }
                    if (currentLine.StartsWith("vt "))
                    {
                        textureCoordinateIndexOffset += 1;
                    }
                    if (currentLine.StartsWith("vn "))
                    {
                        normalIndexOffset += 1;
                    }

                    fileStringBuilder.AppendLine(currentLine);

                    currentLine = streamReader.ReadLine();
                }

                //Close the file
                streamReader.Close();

                string brushString = SerializeOBJBrushes(vertexIndexOffset, textureCoordinateIndexOffset, normalIndexOffset);

                fileStringBuilder.AppendLine("# Converted from " + m_OriginalFilePath);
                fileStringBuilder.Append(brushString);

                fileString = fileStringBuilder.ToString();
            }

            //Actually Write it to a file
            StreamWriter streamWriter = new StreamWriter(filePath);

            streamWriter.Write(fileString);
            streamWriter.Close();
        }
 public void SetMode(string propertyName, SerializeMode mode) {
     Rules[propertyName] = mode;
 }
Exemplo n.º 24
0
 private void SetProperty(string property, SerializeMode mode)
 {
     Rules.SetMode(property, mode);
 }
Exemplo n.º 25
0
        public void Set <T>(string property, SerializeMode mode)
        {
            EntityPropertyRules modes = this[typeof(T)];

            modes.SetMode(property, mode);
        }
Exemplo n.º 26
0
 public SerializationModeAtribute(SerializeMode serializeMode)
 {
     SerializeMode = serializeMode;
 }