Advertisement
Knogle

Simple C based MTA/SA:MP Object Converter

Mar 12th, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. /*
  6. (1) CreateObject
  7. (2) CreateDynamicObject
  8. (3) MTA_SA
  9.  
  10.  
  11. */
  12. int inputtype=0;
  13. int outputtype=0;
  14.  
  15. objecttype()
  16. {
  17.     printf("\nValid Object types\n");
  18.     printf("CreateObject: (1)\n");
  19.     printf("CreateDynamicObject: (2)\n");
  20.     printf("MTA_SA: (3)\n");
  21.    
  22. }
  23.  
  24.  
  25. int main ( int argc, char *argv[] )
  26. {
  27.     printf("\nSimple SA-MP/MTA-SA Object Converter\n");
  28.     printf("\nCopyright (C) 2017  Fabian Druschke (Knogle)");
  29.     printf("\nThis program comes with ABSOLUTELY NO WARRANTY; \nfor details see http://www.gnu.org/licenses/.");
  30.     printf("\nThis is free software, and you are welcome to redistribute it \n \n \n");
  31.  
  32.     if ( argc != 2 )
  33.     {
  34.        
  35.         printf( "Usage: %s [filename]\n", argv[0] );
  36.     }
  37.     else
  38.     {  
  39.         FILE *fIn = fopen(argv[1], "r");
  40.         if ( fIn == 0 )
  41.         {
  42.             printf( "Could not open file\n" );
  43.         }
  44.         else
  45.         {  
  46.            
  47.             int inputtype=0;
  48.             int i=0;
  49.             int model = 0, items = 0;
  50.             float pos[3] = {0}, rot[3] = {0};
  51.            
  52.             objecttype();
  53.             printf("\nSpecify input object type:\n");
  54.             scanf("%i",&inputtype);
  55.             if(inputtype != 1 || inputtype != 2 || inputtype != 3)
  56.             {
  57.                 objecttype();
  58.             }
  59.             int outputtype=0;
  60.             printf("\nSpecify output object type:\n");
  61.             scanf("%i",&outputtype);
  62.             if(outputtype != 1 || inputtype != 2 || inputtype != 3)
  63.             {
  64.                 objecttype();
  65.             }
  66.             if(outputtype == inputtype)
  67.             {
  68.                 printf("You cannot convert objects into the same type!");
  69.             }
  70.             else
  71.             {
  72.                
  73.                
  74.                 FILE *fOut = fopen("output.txt", "wb");
  75.                
  76.                 clock_t t;
  77.                 t = clock();
  78.                 if(inputtype == 1)//CreateObject
  79.                 {
  80.                     while ((items = fscanf(fIn, "%*[^-0123456789]%d%*[,]%f%*[,]%f%*[,]%f%*[,]%f%*[,]%f%*[,]%f%*s", &model, &pos[0], &pos[1], &pos[2], &rot[0], &rot[1], &rot[2])) != EOF)
  81.                     {  
  82.                         if(outputtype == 3)
  83.                         {
  84.                             if (items == 7)
  85.                             {
  86.                                 fprintf(fOut, "<object id=\"object (Konvert)(%d)\" breakable=\"true\" interior=\"0\" collisions=\"true\" alpha=\"255\" model=\"%d\" doublesided=\"false\" scale=\"1\" dimension=\"0\" posX=\"%.7g\" posY=\"%.7g\" posZ=\"%.7g\" rotX=\"%.7g\" rotY=\"%.7g\" rotZ=\"%.7g\"></object>\n", i,model, pos[0], pos[1], pos[2], rot[0], rot[1], rot[2]);
  87.                                 i++;
  88.                             }
  89.                         }
  90.                         if(outputtype == 2)
  91.                         {
  92.                             if (items == 7)
  93.                             {
  94.                                 fprintf(fOut, "CreateDynamicObject(%d,%f,%f,%f,%f,%f,%f);// Object (%d)\n",model, pos[0], pos[1], pos[2], rot[0], rot[1], rot[2],i);
  95.                                 i++;
  96.                             }
  97.                         }
  98.  
  99.                        
  100.                     }
  101.                    
  102.                    
  103.                 }
  104.                 if(inputtype == 2)//CreateDynamicObject
  105.                 {
  106.                     while ((items = fscanf(fIn, "%*[^-0123456789]%d%*[,]%f%*[,]%f%*[,]%f%*[,]%f%*[,]%f%*[,]%f%*s", &model, &pos[0], &pos[1], &pos[2], &rot[0], &rot[1], &rot[2])) != EOF)
  107.                     {  
  108.                         if(outputtype == 3)
  109.                         {
  110.                             if (items == 7)
  111.                             {
  112.                                 fprintf(fOut, "<object id=\"object (Konvert)(%d)\" breakable=\"true\" interior=\"0\" collisions=\"true\" alpha=\"255\" model=\"%d\" doublesided=\"false\" scale=\"1\" dimension=\"0\" posX=\"%.7g\" posY=\"%.7g\" posZ=\"%.7g\" rotX=\"%.7g\" rotY=\"%.7g\" rotZ=\"%.7g\"></object>\n", i,model, pos[0], pos[1], pos[2], rot[0], rot[1], rot[2]);
  113.                                 i++;
  114.                             }
  115.                         }
  116.                         if(outputtype == 1)
  117.                         {
  118.                             if (items == 7)
  119.                             {
  120.                                 fprintf(fOut, "CreateObject(%d,%f,%f,%f,%f,%f,%f);// Object (%d)\n",model, pos[0], pos[1], pos[2], rot[0], rot[1], rot[2],i);
  121.                                 i++;
  122.                             }
  123.                         }
  124.  
  125.                        
  126.                     }
  127.                    
  128.                    
  129.                 }
  130.                 if(inputtype == 3)//MTA_SA
  131.                 {
  132.                     char str1[32], str2[32], str3[32], str4[32], str5[32], str6[32] ,str7[32], str8[32], str9[32];
  133.                     char objstr[32];
  134.                     char line[512];
  135.                      
  136.                     while (fgets(line, 512, fIn) != NULL)
  137.                     {  
  138.                         if(strstr(line,"vehicle") || strstr(line,"ped")  || strstr(line,"removeWorld"))
  139.                         continue;
  140.                         char searchchar = '\"';
  141.                         char replacechar = ' ';
  142.                         char *valueptr;
  143.  
  144.                         valueptr = strchr(line, searchchar);
  145.                         if(valueptr != NULL)
  146.                         *valueptr = replacechar;
  147.                        
  148.  
  149.                         while (valueptr !=NULL)
  150.                         {
  151.                             valueptr = strchr(valueptr + 1, searchchar);
  152.                             if(valueptr != NULL)
  153.                             *valueptr = replacechar;
  154.                         }
  155.                         searchchar = '=';
  156.                         replacechar = ' ';
  157.                        
  158.                         valueptr = strchr(line, searchchar);
  159.                         if(valueptr != NULL)
  160.                         *valueptr = replacechar;
  161.                        
  162.  
  163.                         while (valueptr !=NULL)
  164.                         {
  165.                             valueptr = strchr(valueptr + 1, searchchar);
  166.                             if(valueptr != NULL)
  167.                             *valueptr = replacechar;
  168.                         }
  169.                        
  170.                         if(strstr(line,"collision") && !(str1[0] == '\0'))
  171.                         if(items = sscanf(line,"%*s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s %s %*s %s %*s %s %*s %s %*s %s %*s %s %*s %s",str1,str2,str3,str4,str5,str6  ,str7,str8,str9));
  172.                    
  173.                        
  174.                         if(str1[0] == '\0')
  175.                         if(items = sscanf(line,"%*s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s %s %*s %s %*s %s %*s %s %*s %s %*s %s %*s %s",str1,str2,str3,str4,str5,str6  ,str7,str8,str9));
  176.  
  177.                         if(!strstr(line,"collision") && !(str1[0] == '\0'))
  178.                         if(items = sscanf(line,"%*s %*s %*s %s %*s %*s %*s %*s %*s %*s %*s %*s %s %*s %*s %*s %*s %*s %s %*s %s %*s %s %*s %s %*s %s %*s %s %*s %s",str1,str2,str3,str4,str5,str6  ,str7,str8,str9));
  179.                         long int xmlmodelid = atoi(str2);
  180.                         float xmlPosX= atof(str4);
  181.                         float xmlPosY= atof(str5);
  182.                         float xmlPosZ= atof(str6); 
  183.                         float xmlRotX= atof(str7);
  184.                         float xmlRotY= atof(str8);
  185.                         float xmlRotZ= atof(str9); 
  186.                         {
  187.                            
  188.                             if(outputtype == 2)
  189.                             {
  190.                                 if (items == 9)
  191.                                 {
  192.                                     fprintf(fOut, "CreateDynamicObjectObject(%li,%f,%f,%f,%f,%f,%f);// %s\n",xmlmodelid,xmlPosX, xmlPosY,xmlPosZ,xmlRotX,xmlRotY,xmlRotZ,str1);
  193.                                     printf("CreateDynamicObjectObject(%li,%f,%f,%f,%f,%f,%f);// %s\n",xmlmodelid,xmlPosX, xmlPosY,xmlPosZ,xmlRotX,xmlRotY,xmlRotZ,str1);
  194.                                     i++;
  195.                                 }
  196.                             }
  197.                             if(outputtype == 1)
  198.                             {
  199.                                 if (items == 9)
  200.                                 {
  201.                                     fprintf(fOut, "CreateObject(%li,%f,%f,%f,%f,%f,%f);// %s\n",xmlmodelid,xmlPosX, xmlPosY,xmlPosZ,xmlRotX,xmlRotY,xmlRotZ,str1);
  202.                                     i++;
  203.                                 }
  204.                             }
  205.                         }
  206.  
  207.                        
  208.                     }
  209.                    
  210.                    
  211.                 }
  212.                 t = clock() - t;
  213.                 double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
  214.                 printf("The conversion took %f seconds to finish \n", time_taken);
  215.                 fclose(fOut);
  216.                 fclose(fIn);
  217.                 printf("%d Objekt(e) konvertiert.\n\n",i);
  218.                 return 0;
  219.             }
  220.         }
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement