public static void Serialize(string filePath, DictionarySerializer sTest)
        {
            serializer = new DataContractSerializer(typeof(DictionarySerializer));

            using (var writer = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                serializer.WriteObject(writer, sTest);
            }
        }
Esempio n. 2
0
        public bool Generate(string rootDir, string caseName, bool linkCharacters = true, bool generatePackage = false)
        {
            Directory.CreateDirectory(rootDir);
            if (Directory.Exists(getSubDirFile(getSubDirFile(rootDir, MD5Helper.GetMd5Hash(profile.characterName), true), "casenumber", true)))
            {
                return(false);
            }
            if (!replacement.ContainsKey("code"))
            {
                return(false);
            }
            Dictionary <string, string> fileList = new Dictionary <string, string>();
            SortedList <string, string> links    = new SortedList <string, string>();

            links.Add(MD5Helper.GetMd5Hash(profile.characterName), MD5Helper.GetMd5Hash(profile.characterName));
            StreamWriter writer;

            if (linkCharacters)
            {
                foreach (string alt in profile.altsName)
                {
                    links.Add(MD5Helper.GetMd5Hash(alt), MD5Helper.GetMd5Hash(alt));
                }
                foreach (string alt in profile.altsName)
                {
                    // find the subfolder
                    string uid    = MD5Helper.GetMd5Hash(alt);
                    string folder = getSubDirFile(rootDir, uid, true);
                    // generate links if not exist
                    Directory.CreateDirectory(folder);
                    string linkfile = folder + "info";
                    if (File.Exists(linkfile))
                    {
                        string[]      readin = File.ReadAllLines(linkfile);
                        int           index = 0, line = 0;
                        StringBuilder build = new StringBuilder();
                        List <string> lines = new List <string>();
                        while (index < links.Count || line < readin.Length)
                        {
                            if (index == links.Count)
                            {
                                lines.Add(readin[line++]);
                            }
                            else
                            {
                                string lineExpected;
                                if (links.Keys[index] == uid)
                                {
                                    lineExpected = "CURL|" + uid;
                                }
                                else
                                {
                                    lineExpected = "LINK|" + links.Keys[index];
                                }
                                build.Append(lineExpected + "\n");
                                // check if line matches
                                if (readin[line].Equals(lineExpected))
                                {
                                    lines.Add(readin[line++]);
                                }
                                else
                                {
                                    lines.Add(lineExpected);
                                }
                                index++;
                            }
                        }
                        fileList.Add(linkfile, build.ToString());
                        writer = new StreamWriter(linkfile);
                        foreach (string l in lines)
                        {
                            writer.WriteLine(l);
                        }
                        writer.Close();
                    }
                    else
                    {
                        StringBuilder build = new StringBuilder();
                        int           index = 0;
                        while (index < links.Count)
                        {
                            string lineExpected;
                            if (links.Keys[index] == uid)
                            {
                                lineExpected = "CURL|" + uid;
                            }
                            else
                            {
                                lineExpected = "LINK|" + links.Keys[index];
                            }
                            build.Append(lineExpected + "\n");
                            index++;
                        }
                        fileList.Add(linkfile, build.ToString());
                        writer = new StreamWriter(linkfile);
                        writer.WriteLine(build.ToString());
                        writer.Close();
                    }
                }
            }
            // find the actually charater's subfolder
            string cuid = MD5Helper.GetMd5Hash(profile.characterName);
            // generate link file
            string cfolder = getSubDirFile(rootDir, cuid, true);

            Directory.CreateDirectory(cfolder);
            string clinkfile = cfolder + "info";

            if (File.Exists(clinkfile))
            {
                string[]      readin = File.ReadAllLines(clinkfile);
                int           index = 0, line = 0;
                StringBuilder build = new StringBuilder();
                List <string> lines = new List <string>();
                while (index < links.Count || line < readin.Length)
                {
                    if (index == links.Count)
                    {
                        lines.Add(readin[line++]);
                    }
                    else
                    {
                        string lineExpected;
                        if (links.Keys[index] == cuid)
                        {
                            lineExpected = "CURL|" + cuid;
                        }
                        else
                        {
                            lineExpected = "LINK|" + links.Keys[index];
                        }
                        build.Append(lineExpected + "\n");
                        // check if line matches
                        if (readin[line].Equals(lineExpected))
                        {
                            lines.Add(readin[line++]);
                        }
                        else
                        {
                            lines.Add(lineExpected);
                        }
                        index++;
                    }
                }
                //Add current case
                string caseRecd = "RECD|" + replacement["code"].Replace(" ", string.Empty) + "|" + getDescription(replacement["code"]) + "|" + caseName;
                build.Append(caseRecd + "\n");
                fileList.Add(clinkfile, build.ToString());
                writer = new StreamWriter(clinkfile);
                foreach (string l in lines)
                {
                    writer.WriteLine(l);
                }
                writer.Close();
            }
            else
            {
                StringBuilder build = new StringBuilder();
                int           index = 0;
                while (index < links.Count)
                {
                    string lineExpected;
                    if (links.Keys[index] == cuid)
                    {
                        lineExpected = "CURL|" + cuid;
                    }
                    else
                    {
                        lineExpected = "LINK|" + links.Keys[index];
                    }
                    build.Append(lineExpected + "\n");
                    index++;
                }
                //Add current case
                string caseRecd = "RECD|" + replacement["code"].Replace(" ", string.Empty) + "|" + getDescription(replacement["code"]) + "|" + caseName;
                build.Append(caseRecd + "\n");
                fileList.Add(clinkfile, build.ToString());
                writer = new StreamWriter(clinkfile);
                writer.WriteLine(build.ToString());
                writer.Close();
            }
            // generate case subfolder
            string casefolder = getSubDirFile(cfolder, caseName, true);

            Directory.CreateDirectory(casefolder);
            // generate markdown
            string resultMD = (string)markdownSource.Clone();

            foreach (FieldConfig config in availableFields.Values)
            {
                if (replacement.ContainsKey(config.name))
                {
                    resultMD = resultMD.Replace("$<" + config.replacement + ">", replacement[config.name]);
                }
            }
            // write markdown
            string markdownFilePath = casefolder + "record.md";

            writer = new StreamWriter(markdownFilePath);
            writer.WriteLine(resultMD);
            writer.Close();
            fileList.Add(markdownFilePath, resultMD);
            // generate html
            string resultHtml = Markdown.ToHtml(resultMD);

            // write html
            writer = new StreamWriter(casefolder + "record.html");
            writer.WriteLine(resultHtml);
            writer.Close();
            if (generatePackage)
            {
                // put all link & markdown into a xml file
                string submitPackageFolder = getSubDirFile(rootDir, "submitPackages", true);
                Directory.CreateDirectory(submitPackageFolder);
                DictionarySerializer ds = new DictionarySerializer(fileList);
                // write zip
                DictionarySerializer.Serialize(submitPackageFolder + profile.characterName + "CASE" + caseName + ".pkg", ds);
            }

            return(true);
        }