⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pagesstorageprovider.cs

📁 YetAnotherForum.Net+ScrewTurnWiki中文完美汉化增强版
💻 CS
📖 第 1 页 / 共 3 页
字号:
		/// </summary>
		/// <param name="page">The Page.</param>
		/// <param name="username">The Username.</param>
		/// <param name="subject">The Subject.</param>
		/// <param name="dateTime">The Date/Time.</param>
		/// <param name="body">The Body.</param>
		/// <param name="parent">The Parent Message ID, or -1.</param>
		/// <returns>True if the Message has been added successfully.</returns>
		public bool AddMessage(PageInfo page, string username, string subject, DateTime dateTime, string body, int parent) {
			subject = Tools.EscapeString(subject);
			body = Tools.EscapeString(body);
			StringBuilder sb = new StringBuilder();

			// Structure
			// ID|Username|Subject|DateTime|ParentID|Body

			sb.Append(GetFreeMessageID(page));
			sb.Append("|");
			sb.Append(username);
			sb.Append("|");
			sb.Append(subject);
			sb.Append("|");
			sb.Append(dateTime.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss"));
			sb.Append("|");
			sb.Append(parent.ToString());
			sb.Append("|");
			sb.Append(body);
			sb.Append("\r\n");

			Tools.AppendFile(Settings.MessagesDirectory + ((LocalPageInfo)page).File, sb.ToString());

			return true;
		}

		/// <summary>
		/// Find a free Message ID for a Page.
		/// </summary>
		/// <param name="page">The Page.</param>
		/// <returns>The Message ID.</returns>
		private int GetFreeMessageID(PageInfo page) {
			LocalPageInfo pg = (LocalPageInfo)page;

			if(!File.Exists(Settings.MessagesDirectory + pg.File)) return 0;

			int result = 0;

			string data;
			lock(lockerDisc) {
				data = Tools.LoadFile(Settings.MessagesDirectory + pg.File).Replace("\r", "");
			}

			string[] lines = data.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
			int idx, tmp;
			for(int i = 0; i < lines.Length; i++) {
				idx = lines[i].IndexOf('|');
				tmp = int.Parse(lines[i].Substring(0, idx));
				if(tmp > result) result = tmp;
			}

			result++;

			return result;
		}

		/// <summary>
		/// Removes a Message.
		/// </summary>
		/// <param name="page">The Page.</param>
		/// <param name="id">The ID of the Message to remove.</param>
		/// <param name="removeReplies">A value specifying whether or not to remove the replies.</param>
		/// <returns>True if the Message has been removed successfully.</returns>
		public bool RemoveMessage(PageInfo page, int id, bool removeReplies) {
			List<Message> messages = new List<Message>(GetMessages(page));
			Message msg = FindMessage(messages, id);
			if(msg == null) return false;

			List<Message> replies = null;
			if(!removeReplies) {
				replies = msg.Replies;
			}

			if(!removeReplies && replies.Count > 0) {
				// Find Message's anchestor
				Message anchestor = FindAnchestor(messages, msg.ID);
				if(anchestor != null) anchestor.Replies.AddRange(replies);
				else messages.AddRange(replies);
			}

			RemoveMessage(messages, msg);

			lock(lockerDisc) {
				DumpMessages(page, messages);
			}

			return true;
		}

		/// <summary>
		/// Finds the anchestor/parent of a Message.
		/// </summary>
		/// <param name="messages">The Messages.</param>
		/// <param name="id">The Message ID.</param>
		/// <returns>The anchestor Message or null.</returns>
		private Message FindAnchestor(List<Message> messages, int id) {
			Message result = null;
			for(int i = 0; i < messages.Count; i++) {
				for(int k = 0; k < messages[i].Replies.Count; k++) {
					if(messages[i].Replies[k].ID == id) {
						result = messages[i];
						break;
					}
					if(result == null) {
						result = FindAnchestor(messages[i].Replies, id);
					}
				}
				if(result != null) break;
			}
			return result;
		}

		/// <summary>
		/// Removes a Message from a Message Tree.
		/// </summary>
		/// <param name="messages">The Message Tree.</param>
		/// <param name="msg">The Message to Remove.</param>
		/// <returns>True if the Message has been removed.</returns>
		private bool RemoveMessage(List<Message> messages, Message msg) {
			for(int i = 0; i < messages.Count; i++) {
				if(messages.Contains(msg)) {
					messages.Remove(msg);
					return true;
				}
				bool done = RemoveMessage(messages[i].Replies, msg);
				if(done) return true;
			}
			return false;
		}

		/// <summary>
		/// Modifies a Message.
		/// </summary>
		/// <param name="page">The Page.</param>
		/// <param name="id">The ID of the Message to modify.</param>
		/// <param name="username">The Username.</param>
		/// <param name="subject">The Subject.</param>
		/// <param name="dateTime">The Date/Time.</param>
		/// <param name="body">The Body.</param>
		/// <returns>True if the Message has been modified successfully.</returns>
		public bool ModifyMessage(PageInfo page, int id, string username, string subject, DateTime dateTime, string body) {
			List<Message> messages = new List<Message>(GetMessages(page));

			Message msg = FindMessage(messages, id);
			
			if(msg == null) return false;

			msg.Username = username;
			msg.Subject = subject;
			msg.DateTime = dateTime;
			msg.Body = body;

			lock(lockerDisc) {
				DumpMessages(page, messages);
			}

			return true;
		}

		/// <summary>
		/// Dumps the Message tree of a Page to disk.
		/// </summary>
		/// <param name="page">The Page.</param>
		/// <param name="messages">The Message tree.</param>
		private void DumpMessages(PageInfo page, List<Message> messages) {
			StringBuilder sb = new StringBuilder();
			AppendMessages(messages, -1, sb);
			Tools.WriteFile(Settings.MessagesDirectory + ((LocalPageInfo)page).File, sb.ToString());
		}

		/// <summary>
		/// Appends to a StringBuilder object the branches and leaves of a Message tree.
		/// </summary>
		/// <param name="messages">The Message tree branch to append.</param>
		/// <param name="parent">The ID of the parent of the Message tree or -1.</param>
		/// <param name="sb">The StringBuilder.</param>
		/// <remarks>The methods appends the Messages traversing the tree depht-first, and it is recursive.</remarks>
		private void AppendMessages(List<Message> messages, int parent, StringBuilder sb) {
			// Depht-first

			// Structure
			// ID|Username|Subject|DateTime|ParentID|Body

			for(int i = 0; i < messages.Count; i++) {
				sb.Append(messages[i].ID.ToString());
				sb.Append("|");
				sb.Append(messages[i].Username);
				sb.Append("|");
				sb.Append(Tools.EscapeString(messages[i].Subject));
				sb.Append("|");
				sb.Append(messages[i].DateTime.ToString("yyyy'/'MM'/'dd' 'HH':'mm':'ss"));
				sb.Append("|");
				sb.Append(parent.ToString());
				sb.Append("|");
				sb.Append(Tools.EscapeString(messages[i].Body));
				sb.Append("\r\n");
				AppendMessages(messages[i].Replies, messages[i].ID, sb);
			}
		}

		/// <summary>
		/// Gets all the Navigation Paths.
		/// </summary>
		/// <remarks>The array is unsorted.</remarks>
		public NavigationPath[] AllNavigationPaths {
			get {
				List<NavigationPath> paths = new List<NavigationPath>();

				string[] lines = Tools.LoadFile(Settings.NavigationPathsFile).Replace("\r", "").Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

				// Structure
				// PathName|Page1|Page2|...

				string[] fields;
				for(int i = 0; i < lines.Length; i++) {
					fields = lines[i].Split('|');
					if(fields.Length == 0) continue;
					NavigationPath p = new NavigationPath(fields[0], this);
					for(int k = 1; k < fields.Length; k++) {
						p.Pages.Add(fields[k]);
					}
					paths.Add(p);
				}
				return paths.ToArray();
			}
		}

		/// <summary>
		/// Adds a new Navigation Path.
		/// </summary>
		/// <param name="name">The Name of the Path.</param>
		/// <param name="pages">The Pages array.</param>
		/// <returns>True if the new Path is added successfully.</returns>
		public NavigationPath AddNavigationPath(string name, PageInfo[] pages) {
			StringBuilder sb = new StringBuilder();
			NavigationPath tmp = new NavigationPath(name, this);
			sb.Append("\r\n");
			sb.Append(name);
			for(int i = 0; i < pages.Length; i++) {
				if(pages[i].Provider == this) {
					sb.Append("|");
					sb.Append(pages[i].Name);
					tmp.Pages.Add(pages[i].Name);
				}
			}
			Tools.AppendFile(Settings.NavigationPathsFile, sb.ToString());
			return tmp;
		}

		/// <summary>
		/// Modifies an existing Navigation Path.
		/// </summary>
		/// <param name="name">The Name of the Path to modify.</param>
		/// <param name="pages">The Pages array.</param>
		/// <returns>True if the Path is modified successfully.</returns>
		public NavigationPath ModifyNavigationPath(string name, PageInfo[] pages) {
			NavigationPath[] paths = AllNavigationPaths;
			NavigationPathComparer comp = new NavigationPathComparer();
			NavigationPath tmp = new NavigationPath(name, this);
			for(int i = 0; i < paths.Length; i++) {
				if(comp.Compare(tmp, paths[i]) == 0) {
					paths[i].Pages.Clear();
					NavigationPath np = new NavigationPath(name, this);
					for(int k = 0; k < pages.Length; k++) {
						if(pages[i].Provider == this) {
							np.Pages.Add(pages[k].Name);
						}
					}
					paths[i] = np;
					DumpNavigationPaths(paths);
					return np;
				}
			}
			return null;
		}

		/// <summary>
		/// Removes a Navigation Path.
		/// </summary>
		/// <param name="name">The Name of the Path to remove.</param>
		/// <returns>True if the Path is removed successfully.</returns>
		public bool RemoveNavigationPath(string name) {
			List<NavigationPath> paths = new List<NavigationPath>(AllNavigationPaths);
			NavigationPathComparer comp = new NavigationPathComparer();
			NavigationPath tmp = new NavigationPath(name, this);
			for(int i = 0; i < paths.Count; i++) {
				if(comp.Compare(tmp, paths[i]) == 0) {
					paths.Remove(paths[i]);
					DumpNavigationPaths(paths.ToArray());
					return true;
				}
			}
			return false;
		}

		/// <summary>
		/// Writes an array of Navigation Paths to disk.
		/// </summary>
		/// <param name="paths">The array.</param>
		private void DumpNavigationPaths(NavigationPath[] paths) {
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < paths.Length; i++) {
				sb.Append(paths[i].Name);
				for(int k = 0; k < paths[i].Pages.Count; k++) {
					sb.Append("|");
					sb.Append(paths[i].Pages[k]);
				}
				if(i != paths.Length - 1) sb.Append("\r\n");
			}
			Tools.WriteFile(Settings.NavigationPathsFile, sb.ToString());
		}

		/// <summary>
		/// Gets all the Snippets.
		/// </summary>
		public Snippet[] AllSnippets {
			get {
				List<Snippet> snippets = new List<Snippet>();
				string[] files = Directory.GetFiles(Settings.SnippetsDirectory, "*.cs");
				for(int i = 0; i < files.Length; i++) {
					snippets.Add(new Snippet(Path.GetFileNameWithoutExtension(files[i]), Tools.LoadFile(files[i]), this));
				}
				return snippets.ToArray();
			}
		}

		/// <summary>
		/// Adds a new Snippet.
		/// </summary>
		/// <param name="name">The Name of the Snippet.</param>
		/// <param name="content">The Content of the Snippet.</param>
		/// <returns>The correct Snippet object.</returns>
		public Snippet AddSnippet(string name, string content) {
			Tools.WriteFile(Settings.SnippetsDirectory + name + ".cs", content);
			return new Snippet(name, content, this);
		}

		/// <summary>
		/// Modifies a new Snippet.
		/// </summary>
		/// <param name="name">The Name of the Snippet to modify.</param>
		/// <param name="content">The Content of the Snippet.</param>
		/// <returns>The correct Snippet object.</returns>
		public Snippet ModifySnippet(string name, string content) {
			Tools.WriteFile(Settings.SnippetsDirectory + name + ".cs", content);
			return new Snippet(name, content, this);
		}

		/// <summary>
		/// Removes a new Snippet.
		/// </summary>
		/// <param name="name">The Name of the Snippet to remove.</param>
		/// <returns>True if the Snippet is removed successfully.</returns>
		public bool RemoveSnippet(string name) {
			File.Delete(Settings.SnippetsDirectory + name + ".cs");
			return true;
		}

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -