Ejemplo n.º 1
0
        /// <summary>
        /// Patches a file into a JCR6 resource, if JCR6 can recognise it as a resource.
        /// </summary>
        /// <param name="file">File.</param>
        public void PatchFile(string file)
        {
            JCR6.dCHAT($"Patching: {file}");
            var p = JCR6.Dir(file);

            if (p == null)
            {
                JCR6.JERROR = ("PATCH ERROR:" + JCR6.JERROR);
                return;
            }
            Patch(p);
        }
Ejemplo n.º 2
0
        public override bool Recognize(string file)
        {
            bool ret = false;

            if (!File.Exists(file))
            {
                JCR6.dCHAT(file + " not found!");
                return(false);
            }
            var bt = QuickStream.ReadFile(file);

            ret = bt.Size > 10; // I don't believe a JCR6 file can even get close to that!
            ret = ret && bt.ReadString(checkheader.Length) == checkheader;
            bt.Close();
            //Console.WriteLine(ret);
            return(ret);
        }
Ejemplo n.º 3
0
        /*
         *  The XTag function is need to add extra functionality to JCR Dir and is called by JCR_Dir if these extra functions are actually used.
         *
         *  (Well, the need of this XTag function lead to JCR5 becoming really really ugly, and that eventually lead to me realizing that if JCR5 would ever be able to do more a more flexible format was needed, hence the birth of JCR6. ;)
         *
         */
        void XTag(QuickStream BT, TJCRDIR JCR, bool LoadComments, string JCRFile)
        {
            string XTC;  // Stands for 'XTag Command'. I'm not promoting any sorts of stuff here(in fact I recommend not to use that) :P
            var    Length = BT.ReadInt();

            XTC = BT.ReadString(Length).ToUpper();
            string  F;
            TJCRDIR J;
            var     D = System.IO.Path.GetDirectoryName(JCRFile).Replace(@"\", "/"); //Replace(ExtractDir(JCRFile),"\","/"); If D And Right(D,1)<>"/" D:+"/"

            if (D != "" && qstr.Right(D, 1) != "/")
            {
                D += "/";
            }
            //'JCRD_Print "XTag: "+XTC
            switch (XTC)
            {
            case "IMP":
            case "IMPORT":
                Length = BT.ReadInt();
                F      = BT.ReadString(Length).Replace("\\", "/");
                if (qstr.Left(F, 1) != "/" && qstr.Mid(F, 2, 1) != ":" && System.IO.File.Exists(D + F))
                {
                    F = D + F;       //' If the required file to import is found in the same directory than hit that file.
                }
                J = JCR6.Dir(F);     //' ,LoadComments)
                if (J == null)
                {
                    //'JCRD_Print "WARNING! Could not import "+F+"~n= Report: "+JCRD_DumpError
                }
                else
                {
                    //'JCRD_Print "Importing: "+F
                    //'For Local K$=EachIn MapKeys(J)
                    //'   MapInsert JCR,K,MapValueForKey(J,K)
                    //'   Next
                    JCR.Patch(J);
                }
                break;

            default:
                JCR_JAMERR("WARNING! Unknown XTag in JCR file!", "?", "?", "XTAG");
                break;
            }
        }
Ejemplo n.º 4
0
        private TJCRDIR RDir(string file, bool ap)
        {
            /*
             * // init
             * var ret = new TJCRDIR();
             * var path = file;
             * var w = new List<string>();
             * var di = new DirectoryInfo(path);
             * ret.Comments["Real Dir"] = "Actually \"" + path + "\" is not a JCR6 resource, but a directory \"faked\" into a JCR6 resource.";
             * // Check
             * if (!di.Exists) {
             *  FLError = "UseJCR6.JCR6_RealDir.Dir(\"" + path + "\"): Directory does not exist!";
             *  return null;
             * }
             * // Listout
             * foreach (DirectoryInfo fi in di.GetDirectories()) {
             *  if (allowhidden || fi.Name.Substring(0, 1) != ".") {
             *      JCR6.dCHAT("Recursing: " + fi.Name);
             *      var a = JCR6.Dir(path + "/" + fi.Name);
             *      foreach (string k in a.Entries.Keys) {
             *          var ke = a.Entries[k];
             *          ret.Entries[(fi.Name + "/" + k).ToUpper()] = ke;
             *          ke.Entry = fi.Name + "/" + ke.Entry;
             *          ke.MainFile = qstr.ExtractDir(path) + "/" + fi.Name + "/" + ke.Entry;
             *      }
             *  }
             * }
             * foreach (FileInfo fi in di.GetFiles()) {
             *  if (automerge && JCR6.Recognize(path + "/" + fi.Name) != "NONE") {
             *      var a = JCR6.Dir(path + "/" + fi.Name);
             *      if (a == null) {
             *          Console.WriteLine($"WARNING! Scanning {fi.Name} failed >> {JCR6.JERROR}");
             *      } else {
             *          foreach (string k in a.Entries.Keys) {
             *              var ke = a.Entries[k];
             *              ret.Entries[(fi.Name + "/" + k).ToUpper()] = ke;
             *              ke.Entry = fi.Name + "/" + ke.Entry;
             *              ke.MainFile = path + "/" + ke.Entry;
             *          }
             *          foreach (string k in a.Comments.Keys) { ret.Comments[k] = a.Comments[k]; }
             *      }
             *  } else {
             *      var e = new TJCREntry();
             *      e.Entry = fi.Name;
             *      e.MainFile = path + "/" + fi.Name;
             *      e.Storage = "Store";
             *      e.CompressedSize = (int)fi.Length;
             *      e.Size = (int)fi.Length;
             *      ret.Entries[fi.Name.ToUpper()] = e;
             *  }
             * }
             */
            var ret = new TJCRDIR();
            var dir = FileList.GetTree(file, true, allowhidden);

            ret.Comments["Real Dir"] = "Actually \"" + file + "\" is not a JCR6 resource, but a directory \"faked\" into a JCR6 resource.";
            foreach (string chkfile in dir)
            {
                var mf = $"{file.Replace('\\', '/')}/{chkfile}";
                if (automerge && JCR6.Recognize(mf) != "NONE")
                {
                    var t = JCR6.Dir(mf);
                    if (t == null)
                    {
                        Debug.WriteLine($"Error in auto-merge JCR: {JCR6.JERROR}");
                    }
                    else
                    {
                        foreach (string k in t.Entries.Keys)
                        {
                            var ke = t.Entries[k];
                            ret.Entries[$"{chkfile.ToUpper()}/{k}"] = ke;
                            ke.Entry    = chkfile + "/" + ke.Entry;
                            ke.MainFile = mf; //+ "/" + ke.Entry;
                        }
                    }
                }
                else
                {
                    var e  = new TJCREntry();
                    var fi = new FileInfo(mf);
                    e.Entry          = chkfile; //fi.Name;
                    e.MainFile       = mf;
                    e.Storage        = "Store";
                    e.CompressedSize = (int)fi.Length;
                    e.Size           = (int)fi.Length;
                    e.Offset         = 0;
                    ret.Entries[chkfile.ToUpper()] = e;
                }
            }
            // return the crap
            return(ret);
        }
Ejemplo n.º 5
0
        public override TJCRDIR Dir(string file)
        {
            //Private
            //Function JCR_FetchWAD:TJCRDir(WAD$, SupportLevel= 1)
            var Returner = new TJCRDIR();
            //var Ret:TMap = New TMap; returner.entries = ret
            TJCREntry E;
            var       BT    = QuickStream.ReadFile(file, QuickStream.LittleEndian);
            var       Level = "";

            string[] LevelFiles = { "THINGS", "LINEDEFS", "SIDEDEFS", "VERTEXES", "SEGS", "SSECTORS", "NODES", "SECTORS", "REJECT", "BLOCKMAP", "BEHAVIOR" }; //' All files used in a DOOM/HERETIC/HEXEN level, in which I must note that "BEHAVIOR" is only used in HEXEN.
            if (BT == null)
            {
                JCR6.JERROR = "WAD file could not be read!\n" + file;
                return(null);
            }
            //BT = LittleEndianStream(BT) ' WADs were all written for the MS-DOS platform, which used LittleEndian, so we must make sure that (even if the routine is used on PowerPC Macs) that LittleEndian is used
            //WAD files start with a header that can either be 'IWAD' for main wad files or 'PWAD' for patch WAD files. For JCR this makes no difference it all (it didn't even to WAD for that matter), but this is our only way to check if the WAD loaded is actually a WAD file.
            var Header = BT.ReadString(4);

            switch (Header)
            {
            case "IWAD":
                Returner.Comments["Important notice"] = "The WAD file you are viewing is an IWAD,\nmeaning it belongs to a copyrighted project.\n\nAll content within it is very likely protected by copyright\neither by iD software or Apogee's Developers of Incredible Power or Raven Software.\n\nNothing can stop you from analysing this file and viewing its contents,\nbut don't extract and distribute any contents of this file\nwithout proper permission from the original copyright holder";
                break;

            case "PWAD":
                Returner.Comments["Notice"] = "This WAD file is a PWAD or Patch-WAD. It's not part of any official file of the games using the WAD system. Please respect the original copyright holders copyrights though!";
                break;

            default:
                JCR6.JERROR = "JCR_FetchWAD('" + file + "'): Requested file is not a WAD file";
                return(null);
            }
            Returner.CFGbool["__CaseSensitive"] = false;
            //'Next in the WAD files are 2 32bit int values telling how many files the WAD file contains and where in the WAD file the File Table is stored
            var FileCount = BT.ReadInt();
            var DirOffset = BT.ReadInt();

            //DebugLog "This WAD contains "+FileCount+" entries starting at "+DirOffset
            BT.Position = DirOffset;
            // And let's now read all the crap
            for (int Ak = 1; Ak <= FileCount; Ak++)
            {
                //    'DebugLog "Reading entry #"+Ak
                E = new TJCREntry();
                // E.PVars = New StringMap ' Just has to be present to prevent crashes in viewer based software. // Not an issue in the C# version.
                E.MainFile       = file;
                E.Offset         = BT.ReadInt();
                E.Size           = BT.ReadInt();
                E.Entry          = BT.ReadString(8).Trim().Replace("\0", ""); //Replace(Trim(ReadString(BT, 8)), Chr(0), "")
                E.CompressedSize = E.Size;
                E.Storage        = "Store";                                   // WAD does not support compression, so always deal this as "Stored"
                //    'E.Encryption = 0  ' WAD does not support encryption, so always value 0
                if (SupportLevel)                                             // ' If set the system will turn DOOM levels into a folder for better usage. When unset the system will just dump everything together with not the best results, but hey, who cares :)
                {
                    //'Print "File = "+E.FileName+" >> Level = ~q"+Level+"~q >> Len="+Len(E.FileName)+" >> 1 = "+Left(E.FileName,1)+" >> 3 = "+Mid(E.FileName,3,1)
                    //'If Level=""
                    if (qstr.Left(E.Entry, 3) == "MAP")
                    {
                        Level = "MAP_" + E.Entry + "/";
                    }
                    else if (E.Entry.Length == 4 && qstr.Left(E.Entry, 1) == "E" && qstr.Mid(E.Entry, 3, 1) == "M")
                    {
                        Level = "MAP_" + E.Entry + "/";
                    }
                    else if (Level != "")
                    {
                        var Ok = false;
                        foreach (string S in LevelFiles)
                        {
                            Ok = Ok || E.Entry == S;
                            //'Print "Comparing "+E.FileName+" with "+S+"   >>>> "+Ok
                        }
                        if (Ok)
                        {
                            E.Entry = Level + E.Entry;
                        }
                        else
                        {
                            Level = "";
                        }
                    }
                }
                JCR6.dCHAT("Adding: " + E.Entry);
                Returner.Entries[E.Entry.ToUpper()] = E;
            }
            BT.Close();
            //Return Ret
            return(Returner);
        }
Ejemplo n.º 6
0
        public override TJCRDIR Dir(string file)
        {
            QuickStream BT = null;

            try {
                BT = QuickStream.ReadFile(file);
                var    ret = new TJCRDIR();
                string s;
                do
                {
                    if (BT.EOF)
                    {
                        throw new Exception("JQL heading not found!");
                    }
                    s = RL(BT);
                } while (s == "" || qstr.Prefixed(s, "#"));
                if (s != "JQL")
                {
                    throw new Exception("JQL not properly headed!");
                }
                var optional = true;
                var author   = "";
                var notes    = "";
                while (!BT.EOF)
                {
                    s = RL(BT);
                    var c = new QP(s);
                    if (s != "" && (!qstr.Prefixed(s, "#")))
                    {
                        switch (c.commando)
                        {
                        case "REQUIRED":
                        case "REQ":
                            optional = false;
                            break;

                        case "OPTIONAL":
                        case "OPT":
                            optional = true;
                            break;

                        case "PATCH": {
                            var to = c.parameter.IndexOf('>');
                            if (to < 0)
                            {
                                var p = JCR6.Dir(c.parameter);
                                if (p == null)
                                {
                                    if (optional)
                                    {
                                        break;
                                    }
                                    throw new Exception($"Patch error {JCR6.JERROR}");
                                }
                                ret.Patch(p);
                            }
                            else
                            {
                                var rw = c.parameter.Substring(0, to).Trim().Replace("\\", "/");
                                var tg = c.parameter.Substring(to + 1).Trim().Replace("\\", "/");
                                var p  = JCR6.Dir(rw);
                                if (p == null)
                                {
                                    if (optional)
                                    {
                                        break;
                                    }
                                    throw new Exception($"Patch error {JCR6.JERROR}");
                                }
                                ret.Patch(p, tg);
                            }
                            break;
                        }

                        case "AUTHOR":
                        case "AUT":
                            author = c.parameter;
                            break;

                        case "NOTES":
                        case "NTS":
                            notes = c.parameter;
                            break;

                        case "RAW": {
                            var p  = c.parameter.IndexOf('>');
                            var rw = c.parameter.Replace("\\", "/");
                            var tg = rw;
                            if (p >= 0)
                            {
                                rw = c.parameter.Substring(0, p).Trim().Replace("\\", "/");
                                tg = c.parameter.Substring(p + 1).Trim().Replace("\\", "/");
                            }
                            if (tg.Length > 1 && tg[1] == ':')
                            {
                                tg = tg.Substring(2);
                            }
                            while (tg[1] == '/')
                            {
                                tg = tg.Substring(1);
                            }
                            if (rw == "")
                            {
                                throw new Exception("RAW no original");
                            }
                            if (tg == "")
                            {
                                throw new Exception("RAW no target");
                            }
                            if (!File.Exists(rw))
                            {
                                if (optional)
                                {
                                    break;
                                }
                                throw new Exception($"Required raw file \"{rw}\" doesn't exist!");
                            }
                            var e = new TJCREntry();
                            e.Entry                   = tg;
                            e.MainFile                = rw;
                            e.Storage                 = "Store";
                            e.Offset                  = 0;
                            e.Size                    = (int)new FileInfo(rw).Length;
                            e.CompressedSize          = e.Size;
                            e.Notes                   = notes;
                            e.Author                  = author;
                            ret.Entries[tg.ToUpper()] = e;
                            break;
                        }

                        case "TEXT":
                        case "TXT": {
                            var tg = c.parameter.Trim().Replace("\\", "/");
                            if (tg.Length > 1 && tg[1] == ':')
                            {
                                tg = tg.Substring(2);
                            }
                            while (tg[1] == '/')
                            {
                                tg = tg.Substring(1);
                            }
                            if (tg == "")
                            {
                                throw new Exception("TEXT no target");
                            }
                            var e   = new TJCREntry();
                            var buf = new byte[5];
                            e.Entry    = tg;
                            e.MainFile = file;
                            e.Storage  = "Store";
                            e.Offset   = (int)BT.Position;
                            e.Notes    = notes;
                            e.Author   = author;
                            do
                            {
                                if (BT.EOF)
                                {
                                    throw new Exception("Unexpected end of file (TXT Block not ended)");
                                }
                                for (int i = 0; i < 4; i++)
                                {
                                    buf[i] = buf[i + 1];
                                }
                                buf[4] = BT.ReadByte();
                                //Console.WriteLine(Encoding.UTF8.GetString(buf, 0, buf.Length));
                            } while (Encoding.UTF8.GetString(buf, 0, buf.Length) != "@END@");
                            RL(BT);
                            e.Size                    = (int)(BT.Position - 7) - e.Offset;
                            e.CompressedSize          = e.Size;
                            ret.Entries[tg.ToUpper()] = e;
                            break;
                        }

                        case "COMMENT":
                        case "CMT": {
                            if (c.parameter == "")
                            {
                                throw new Exception("Comment without a name");
                            }
                            var cmt = new StringBuilder("");
                            var l   = "";
                            do
                            {
                                if (BT.EOF)
                                {
                                    throw new Exception("Unexpected end of file (COMMENT block not ended)");
                                }
                                l = RL(BT, false);
                                if (l.Trim() != "@END@")
                                {
                                    cmt.Append($"{l}\n");
                                }
                            } while (l.Trim() != "@END@");
                            ret.Comments[c.parameter] = cmt.ToString();
                            break;
                        }

                        case "IMPORT":
                            ret.PatchFile(c.parameter);
                            break;

                        case "END":
                            return(ret);

                        default: throw new Exception($"Unknown instruction! {c.commando}");
                        }
                    }
                }
                return(ret);
            } catch (Exception e) {
                JCR6.JERROR = $"JQL error: {e.Message}";
#if DEBUG
                Console.WriteLine(e.StackTrace);
#endif
                return(null);
            } finally {
                if (BT != null)
                {
                    BT.Close();
                }
            }
        }
Ejemplo n.º 7
0
        private TJCRDIR RDir(string file, bool ap)
        {
            // init
            var ret  = new TJCRDIR();
            var path = file;
            var w    = new List <string>();
            var di   = new DirectoryInfo(path);

            ret.Comments["Real Dir"] = "Actually \"" + path + "\" is not a JCR6 resource, but a directory \"faked\" into a JCR6 resource.";
            // Check
            if (!di.Exists)
            {
                FLError = "UseJCR6.JCR6_RealDir.Dir(\"" + path + "\"): Directory does not exist!";
                return(null);
            }
            // Listout
            foreach (DirectoryInfo fi in di.GetDirectories())
            {
                if (allowhidden || fi.Name.Substring(0, 1) != ".")
                {
                    JCR6.dCHAT("Recursing: " + fi.Name);
                    var a = JCR6.Dir(path + "/" + fi.Name);
                    foreach (string k in a.Entries.Keys)
                    {
                        var ke = a.Entries[k];
                        ret.Entries[(fi.Name + "/" + k).ToUpper()] = ke;
                        ke.Entry    = fi.Name + "/" + ke.Entry;
                        ke.MainFile = path + "/" + fi.Name + "/" + ke.Entry;
                    }
                }
            }
            foreach (FileInfo fi in di.GetFiles())
            {
                if (automerge && JCR6.Recognize(path + "/" + fi.Name) != "NONE")
                {
                    var a = JCR6.Dir(path + "/" + fi.Name);
                    if (a == null)
                    {
                        Console.WriteLine($"WARNING! Scanning {fi.Name} failed >> {JCR6.JERROR}");
                    }
                    else
                    {
                        foreach (string k in a.Entries.Keys)
                        {
                            var ke = a.Entries[k];
                            ret.Entries[(fi.Name + "/" + k).ToUpper()] = ke;
                            ke.Entry    = fi.Name + "/" + ke.Entry;
                            ke.MainFile = path + "/" + ke.Entry;
                        }
                        foreach (string k in a.Comments.Keys)
                        {
                            ret.Comments[k] = a.Comments[k];
                        }
                    }
                }
                else
                {
                    var e = new TJCREntry();
                    e.Entry          = fi.Name;
                    e.MainFile       = path + "/" + fi.Name;
                    e.Storage        = "Store";
                    e.CompressedSize = (int)fi.Length;
                    e.Size           = (int)fi.Length;
                    ret.Entries[fi.Name.ToUpper()] = e;
                }
            }



            // return the crap
            return(ret);
        }