using System; using System.IO; using System.Linq; namespace JD.Core { /// /// The project id is used to find the data corresponding to an executable. /// public static class ProjectId { private static string _value; private static string _location; /// /// Full project id. Example: JD2017_Main /// public static string Value { get { if (_value == null) { return _value = ReadProjectId(); } return _value; } } /// /// Game name is the first part of the id /// public static string GameName => Value.Split('_').First(); /// /// Branch name is the second part of the id /// public static string BranchName => Value.Substring(GameName.Length + 1); /// /// Location where to look for ProjectId.txt, default is current assembly location /// public static string Location { get { if (string.IsNullOrEmpty(_location)) { _location = Path.GetDirectoryName(typeof(ProjectId).Assembly.Location()); } return _location; } set { _location = value; _value = ReadProjectId(); } } private static string ReadProjectId() { var projectIdFile = Path.Combine(Location, "ProjectId.txt"); if (!File.Exists(projectIdFile)) { throw new InvalidProgramException($"Cannot find file {projectIdFile} so the initialization of the data path cannot be done."); } try { var projectId = File.ReadAllText(projectIdFile).Trim(); return projectId; } catch (IOException) { throw new InvalidProgramException($"Cannot read file {projectIdFile} so the initialization of the data path cannot be done."); } } } }