Пример #1
0
        public static void AppendAsTypeName(StringBuilder sb, string shortFileName)
        {
            // alien.tables_01.sxl becomes IAlienTables01

            int len = sb.Length;

            NAMESPACE_PARSE_STATE state = NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar;

            int    lastDot = shortFileName.LastIndexOf('.');
            string src     = lastDot >= 0 ? shortFileName.Substring(0, lastDot) : shortFileName;

            foreach (char c in src)
            {
                if (c == '.')
                {
                    switch (state)
                    {
                    case NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar:
                        throw new Exception(string.Format("Cannot map filename to interface name. Unexpected dot in {0}", shortFileName));

                    case NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters:
                        state = NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar;
                        break;
                    }
                }
                else if (IsCapital(c))
                {
                    sb.Append(c);
                    state = NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters;
                }
                else if (IsLowerCase(c))
                {
                    switch (state)
                    {
                    case NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar:
                        sb.Append((char)(c & ~32));     // Capitalize
                        state = NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters;
                        break;

                    case NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters:
                        sb.Append(c);
                        break;
                    }
                }
                else if (IsDigit(c))
                {
                    switch (state)
                    {
                    case NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar:
                        sb.Append((char)(c & ~32));     // Capitalize
                        break;

                    case NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters:
                        sb.Append(c);
                        break;
                    }
                }
                else
                {
                    // All other characters are ignored
                }
            }

            if (sb.Length - len < 3)
            {
                // Sanity check, don't let artists create IMu as an interface name, it's inpolite
                throw new Exception("Need at least 4 characters in an interface name. Use a more descriptive filename");
            }
        }
Пример #2
0
        public static void AppendPathStringAsNamespace(StringBuilder sb, string filename)
        {
            // Example \models.Skeleton\bones.sxl should map to Asset.Models.Skeleton

            int lastSlash = filename.LastIndexOf('\\');

            if (lastSlash == -1)
            {
                throw new Exception(string.Format("Cannot parse {0} into a namespace. No directory slash", filename));
            }

            sb.Append("Asset.");

            NAMESPACE_PARSE_STATE state = NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar;

            int len = sb.Length;

            for (int i = 0; i < lastSlash; i++)
            {
                char c = filename[i];
                if (c == '\\' || c == '/')
                {
                    switch (state)
                    {
                    case NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar:
                        throw new Exception(string.Format("Cannot parse namespace from {0}. Unexpected slash at pos {1}", filename, i));

                    case NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters:
                        sb.Append('.');
                        state = NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar;
                        break;
                    }
                }
                else if (c == '.')
                {
                    switch (state)
                    {
                    case NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar:
                        throw new Exception(string.Format("Cannot parse namespace from {0}. Unexpected '.' at pos {1}", filename, i));

                    case NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters:
                        sb.Append('.');
                        state = NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar;
                        break;
                    }
                }
                else if (IsCapital(c) || IsLowerCase(c))
                {
                    switch (state)
                    {
                    case NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar:
                        sb.Append((char)(c & ~32));     // That appended a capitalized character
                        state = NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters;
                        break;

                    case NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters:
                        sb.Append(c);
                        break;
                    }
                }
                else
                {
                    switch (state)
                    {
                    case NAMESPACE_PARSE_STATE.ReadingFirstSubspaceChar:
                        throw new Exception(string.Format("Cannot parse namespace from {0}. Unexpected character at pos {1}", filename, i));

                    case NAMESPACE_PARSE_STATE.ReadingSubspaceTrailingCharacters:
                        if (IsDigit(c))
                        {
                            sb.Append(c);
                        }
                        else
                        {
                            throw new Exception(string.Format("Cannot parse namespace from {0}. Unexpected character at pos {1}", filename, i));
                        }
                        break;
                    }
                }
            }

            if (len == sb.Length)
            {
                throw new Exception("No useful characters were parsed to generate a namespace");
            }
        }