Recent Posts
C# | 7 sec ago
None | 16 sec ago
None | 17 sec ago
VB.NET | 38 sec ago
None | 2 min ago
None | 2 min ago
Java | 2 min ago
C++ | 2 min ago
PHP | 2 min ago
JavaScript | 2 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 20th of Apr 2009 09:04:25 PM Download | Raw | Embed | Report
  1. // /home/hannes/Dokumente/c#-Projects/DemoRename/DemoRename/DemoHandler.cs created with MonoDevelop
  2. // User: hannes at 04:12 30.10.2007
  3. //
  4. // To change standard headers go to Edit->Preferences->Coding->Standard Headers
  5. //
  6.  
  7. using System;
  8. using System.IO;
  9. using System.Text;
  10. using System.Collections;
  11. using System.Text.RegularExpressions;
  12.  
  13. namespace DemoRename
  14. {
  15.  
  16.  
  17. public class DemoHandler
  18. {
  19.  
  20. private static string[] _mapNames = new string[]
  21. {
  22. "salvage",
  23. "volcano",
  24. "area22",
  25. "island",
  26. "ark",
  27. "sewer",
  28. "canyon",
  29. "outskirts",
  30. "quarry",
  31. "refinery",
  32. "slipgate",
  33. "valley"
  34. };
  35.  
  36. public static void GetDemoInfos(
  37. String demoFilepath, double hourOffset,
  38. out string newFilename, out string mapName, out PlayerList playerList, out ArrayList teamNameList
  39. )
  40. {
  41.  
  42. mapName = "";
  43. playerList = new PlayerList();
  44.  
  45. using (StreamReader sr = new StreamReader(demoFilepath))
  46. {
  47. bool mapTagFound = false;
  48. string demoContent = sr.ReadToEnd();
  49. string[] demoParts = demoContent.Split('');
  50.  
  51. foreach (String rawData in demoParts)
  52. {
  53. if (!mapTagFound && (rawData.IndexOf("si_map") > -1))
  54. {
  55. string[] mapParts = rawData.Split('/');
  56.  
  57. for (int i = 0; i < mapParts.Length; i++)
  58. {
  59. if (mapParts[i].IndexOf("si_map") > -1)
  60. {
  61. mapTagFound = true;
  62. mapName = FindMapName(mapParts[i + 1]);
  63. break; // JB : can break right ?
  64. }
  65. }
  66. }
  67.  
  68. if (rawData.IndexOf("ui_name") > 0)
  69. {
  70. string[] playerParts = rawData.Split('\n');
  71.  
  72. for (int i = 0; i < playerParts.Length; i++)
  73. {
  74. if (playerParts[i].IndexOf("ui_realname") > -1)
  75. {
  76. string[] nameParts = playerParts[i].Split('\0');
  77.  
  78. string playerName = RemoveColorCodes(nameParts[nameParts.Length-1]);
  79.  
  80. playerList.Add(playerName, new Player(playerName));
  81. }
  82. }
  83.  
  84. }
  85. }
  86.  
  87. } // using (StreamReader sr = new StreamReader(demoFilepath))
  88.  
  89. DateTime demoDate = (new FileInfo(demoFilepath)).LastWriteTime.AddHours(hourOffset);
  90. string dateString = demoDate.ToString("yyMMdd@HH;mm;ss"); // @;; (HH in upper case for 24 hours and not 12)
  91. newFilename = string.Format("{0}_{1}.ndm", dateString, mapName); // builds the new demo filename
  92.  
  93. teamNameList = GetTeamNames(playerList);
  94.  
  95. }
  96.  
  97. private static ArrayList GetTeamNames(PlayerList playerList)
  98. {
  99. ArrayList nameList = new ArrayList();
  100.  
  101. foreach(String key in playerList.Keys)
  102. {
  103. nameList.Add(playerList[key].Name);
  104. }
  105.  
  106. ArrayList teamNameList = new ArrayList();
  107. string nameI = null;
  108. string nameJ = null;
  109.  
  110. for(int i=0; i < nameList.Count; i++)
  111. {
  112. nameI = (string)nameList[i];
  113.  
  114. for(int j=0; j < nameList.Count; j++)
  115. {
  116. if(i != j)
  117. {
  118. nameJ = (string)nameList[j];
  119.  
  120. FindTeamName(teamNameList, nameI, nameJ);
  121. }
  122. }
  123. }
  124.  
  125. return teamNameList;
  126. }
  127.  
  128. private static void FindTeamName(ArrayList teamNameList, String x, String y)
  129. {
  130. int xLength = x.Length, yLength = y.Length;
  131.  
  132. StringBuilder nameBuilder = new StringBuilder();
  133.  
  134. int len = 0;
  135.  
  136. if(xLength < yLength)
  137. {
  138. len = xLength;
  139. }
  140. else
  141. {
  142. len = yLength;
  143. }
  144.  
  145. // von vorne
  146. for( int i=0; i <= len; i++)
  147. {
  148. if(x[i].Equals(y[i]))
  149. {
  150. nameBuilder.Append(x[i]);
  151. }
  152. else
  153. {
  154. break;
  155. }
  156. }
  157.  
  158. if(nameBuilder.Length == 0)
  159. {
  160. // von hinten
  161. for(int m = len; m <= 1; m--)
  162. {
  163. if(x[m].Equals(y[m]))
  164. {
  165. nameBuilder.Append(x[m]);
  166. }
  167. else
  168. {
  169. break;
  170. }
  171. }
  172.  
  173. }
  174.  
  175. string teamName = nameBuilder.ToString();
  176. if(teamName.Length > 0 && !teamNameList.Contains(teamName) )
  177. {
  178. teamNameList.Add(teamName);
  179. }
  180. }
  181.  
  182. private static string FindMapName(string rawString)
  183. {
  184. string mapName = "";
  185.  
  186. foreach(string name in _mapNames)
  187. {
  188. if(rawString.IndexOf(name) > -1)
  189. {
  190. mapName = name;
  191. break;
  192. }
  193. }
  194.  
  195. return mapName;
  196. }
  197.  
  198. ///
  199. /// Cleans a String from colorcodes
  200. ///
  201. private static string RemoveColorCodes(String gameText)
  202. {
  203. if (gameText == null)
  204. {
  205. return String.Empty;
  206. }
  207. else
  208. {
  209. return Regex.Replace(gameText.Trim(), @"(\^\D)|(\$\D)|(\^\d)|(\$\d)", string.Empty);
  210. }
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. }
  219. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: