Example #1
0
        public static string GetObjModelExtra(SetObject obj,
                                              SetObjectType type, string startName = null)
        {
            // Get the correct extra's model name
            string mdlName = startName;

            foreach (var extra in type.Extras)
            {
                if (extra.Type.ToLower() != "model")
                {
                    continue;
                }

                if (string.IsNullOrEmpty(extra.Condition) ||
                    LuaScript.EvaluateObjectCondition(obj, type, extra.Condition))
                {
                    mdlName = extra.Value;
                    break;
                }
            }

            if (string.IsNullOrEmpty(mdlName))
            {
                return(mdlName);
            }

            // If the model name is actually supposed to be the value of
            // another parameter (e.g. in Gismos), get the name from that instead.
            if (mdlName.IndexOf('.') == -1)
            {
                int mdlNameParamIndex = type.GetParameterIndex(mdlName);
                if (mdlNameParamIndex != -1)
                {
                    mdlName = (obj.Parameters[
                                   mdlNameParamIndex].Data as string);
                }
            }
            else
            {
                int openIndex  = mdlName.IndexOf('{');
                int closeIndex = mdlName.IndexOf('}');

                if (openIndex != -1 && closeIndex > openIndex)
                {
                    ++openIndex;
                    if (int.TryParse(mdlName.Substring(openIndex,
                                                       closeIndex - openIndex), out int index) &&
                        index >= 0 && index < type.Parameters.Count)
                    {
                        mdlName = mdlName.Replace($"{{{index}}}",
                                                  (obj.Parameters[index].Data as string));
                    }
                }
            }

            return(mdlName);
        }