//this ctor is used when creating new item of known type public FileNode(FilesModel model, string name, EFileType type) { _model = model; _type = type; _SetName(name); _id = _model.AddGetId(this); }
//currently not used //this ctor is used when creating new item of known type //public FileNode(FilesModel model, string name, EFileType type, string linkTarget = null) //{ // _model = model; // _type = type; // _name = name; // _id = _model.AddGetId(this); // _CtorMisc(linkTarget); //} //this ctor is used when creating new item, importing items from files etc. //name is filename with extension. //sourcePath is used when need to get file text to detect type. //template used when creating new item, to detect item type. public FileNode(FilesModel model, string name, string sourcePath, bool isFolder, string linkTarget = null, string template = null) { //detect file type EFileType type; if (isFolder) { type = EFileType.Folder; } else if (template == "Script.cs") { type = EFileType.Script; } else if (template == "Class.cs" || template == "Partial.cs") { type = EFileType.Class; } else { type = DetectFileType(sourcePath); } _model = model; _type = type; _name = name; _id = _model.AddGetId(this); _CtorMisc(linkTarget); }
//this ctor is used when reading files.xml FileNode(XmlReader x, FileNode parent, FilesModel model) { _model = model; if (parent == null) //the root node { if (x.Name != "files") { throw new ArgumentException("XML root element name must be 'files'"); } x["max-i"].ToInt(out uint u); _model.MaxId = u; } else { _type = x.Name switch { "d" => EFileType.Folder, "s" => EFileType.Script, "c" => EFileType.Class, "n" => EFileType.NotCodeFile, _ => throw new ArgumentException("XML element name must be 'd', 's', 'c' or 'n'"), }; uint id = 0, testScriptId = 0; string linkTarget = null, icon = null; while (x.MoveToNextAttribute()) { var v = x.Value; switch (x.Name) { case "n": _name = v; break; case "i": v.ToInt(out id); break; case "f": _flags = (_Flags)v.ToInt(); break; case "path": linkTarget = v; break; case "icon": icon = v; break; case "run": v.ToInt(out testScriptId); break; } } if (_name.NE()) { throw new ArgumentException("no 'n' attribute in XML"); } _id = _model.AddGetId(this, id); _CtorMisc(linkTarget); if (icon != null && linkTarget == null) { _iconOrLinkTarget = icon; } if (testScriptId != 0) { _testScriptId = testScriptId; } } }
//this ctor is used when copying or importing a workspace. //Deep-copies fields from f, except _model, _name, _id (generates new) and _testScriptId. FileNode(FilesModel model, FileNode f, string name) { _model = model; _name = name; _type = f._type; _state = f._state; _flags = f._flags; _iconOrLinkTarget = f._iconOrLinkTarget; _id = _model.AddGetId(this); }
//this ctor is used when copying or importing a workspace. //Deep-copies fields from f, except _model, _name, _id (generates new) and _testScriptId. FileNode(FilesModel model, FileNode f, string name) { _model = model; _SetName(name); _type = f._type; _state = f._state; _flags = f._flags; _linkTarget = f._linkTarget; _icon = f.CustomIconName; _id = _model.AddGetId(this); }
//this ctor is used when importing items from files etc. //name is filename with extension. //sourcePath is used to get file text to detect type when !isFolder. public FileNode(FilesModel model, string name, string sourcePath, bool isFolder, string linkTarget = null) { _model = model; _type = isFolder ? EFileType.Folder : _DetectFileType(sourcePath); _SetName(name); _id = _model.AddGetId(this); if (!linkTarget.NE() && !isFolder) { _linkTarget = linkTarget; } }
//this ctor is used when reading files.xml FileNode(XmlReader x, FileNode parent, FilesModel model) { _model = model; if (parent == null) //the root node { if (x.Name != "files") { throw new ArgumentException("XML root element name must be 'files'"); } x["max-i"].ToInt(out uint u); _model.MaxId = u; } else { _type = XmlTagToFileType(x.Name, canThrow: true); uint id = 0; while (x.MoveToNextAttribute()) { var v = x.Value; if (v.NE()) { continue; } switch (x.Name) { case "n": _SetName(v); break; case "i": v.ToInt(out id); break; case "f": _flags = (_Flags)v.ToInt(); break; case "path": if (!IsFolder) { _linkTarget = v; } break; case "icon": _icon = v; break; case "run": v.ToInt(out _testScriptId); break; } } if (_name == null) { throw new ArgumentException("no 'n' attribute in XML"); } _id = _model.AddGetId(this, id); } }