November 25, 2020, 05:06:15 AM

Author Topic: [bb] USERLIB DECLS (DLL) TO CSharp (Unity 3D) converter by ZJP [ 1+ years ago ]  (Read 752 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : USERLIB DECLS (DLL) TO CSharp (Unity 3D) converter
Author : ZJP
Posted : 1+ years ago

Description : convert DECLS blitz3d file to standard DllImport instructions

Code :
Code: BlitzBasic
  1. ;
  2. ; Blitz3D USERLIB DECLS (DLL) TO CSharp (Unity 3D) (c) 2015  ZJP  
  3. ; PUBLIC DOMAIN
  4. ;
  5. Dim parameter$(10)
  6. Global i%
  7. Global DECLS_INPUT_FILE$
  8. Global CPP_OUTPUT_FILE$
  9. Global DECLS_FIRST_CAR$
  10. Global line_input_file$
  11. Global Found_char%
  12. Global Alias_Dll$
  13. Global middle$
  14. Global tempo_string$
  15. Global car$
  16. Global nb_param%
  17. Global DLL_lib$
  18. Global CALLING_example$
  19. Global tempo$
  20. Global PROC_Def$
  21. Global Position%
  22. Global Var_TYPE$
  23. Global bof$
  24. Global ok$
  25. ;
  26.  
  27. ; Fichier DECLS - DECLS File <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Change this !!!
  28. DECLS_INPUT_FILE = "Cheetah2.DECLS"
  29.  
  30. ; Lines begin with...<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Change this !!!
  31. DECLS_FIRST_CAR = "xdb"
  32.  
  33. ; DLL Name<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Change this !!!
  34. DLL_lib = "Cheetah2"
  35.  
  36.  
  37. CPP_OUTPUT_FILE = DLL_lib + ".cs"
  38. ;
  39. file_in%  = OpenFile(DECLS_INPUT_FILE)
  40. file_out% = WriteFile(CPP_OUTPUT_FILE)
  41. ;
  42. WriteLine(file_out,"")
  43. WriteLine(file_out,"using UnityEngine;")
  44. WriteLine(file_out,"using System.Collections;")
  45. WriteLine(file_out,"using System.Runtime.InteropServices;")
  46. WriteLine(file_out,"")
  47. WriteLine(file_out,"public class " + DLL_lib + " : MonoBehaviour")
  48. WriteLine(file_out,"{")
  49.  
  50.  
  51. ;
  52. While Not Eof(file_in)
  53.  
  54.    line_input_file$ = ReadLine$( file_in )
  55.  
  56.         ; Remove all spaces in the input line
  57.         tempo=""
  58.         For i=1 To Len(line_input_file$)
  59.                 car$=Mid(line_input_file$,i,1)
  60.                 If car$<>" " Then tempo$=tempo$+car$
  61.         Next
  62.         line_input_file$=tempo$
  63.  
  64.         ; Select only the line began with "px" in this example
  65.    If Left(line_input_file, Len(DECLS_FIRST_CAR)) <> DECLS_FIRST_CAR Then Goto suivant
  66.        
  67.  
  68.    ;  find the Alias
  69.    Found_char = Instr(line_input_file, ":", 1)
  70.    Alias_Dll = Mid(line_input_file, Found_char + 1)
  71.  
  72.  
  73. ;   CALLING_example = "[DllImport (" + Chr(34) + DLL_lib + Chr(34) + ", EntryPoint = " + Alias_Dll + ")]" + Chr(13) + Chr(9) + Chr(9) + "private static extern "
  74.    CALLING_example = Chr(9)+ "[DllImport (" + Chr(34) + DLL_lib + Chr(34) + ", EntryPoint = " + Alias_Dll + ")] private static extern "
  75.    middle = " "
  76.    
  77.    ; Var_TYPE >> % # Or $ *
  78.    Found_char = Instr(line_input_file, "(", 1)
  79.    Var_TYPE = Mid(line_input_file, Found_char - 1, 1)
  80.  
  81.    ; % Integer
  82.    If Var_TYPE = "%" Then
  83.        tempo = Left(line_input_file, Found_char - 2)
  84.        CALLING_example = CALLING_example + "int "
  85.    End If
  86.    
  87.    ; # Float
  88.    If Var_TYPE = "#" Then
  89.        tempo = Left(line_input_file, Found_char - 2)
  90.        CALLING_example = CALLING_example + "float "
  91.    End If
  92.    
  93.    ; $ STRING
  94.    If Var_TYPE = "$" Then
  95.        tempo = Left(line_input_file, Found_char - 2)
  96.        CALLING_example = CALLING_example + "string "
  97.     End If
  98.    
  99.    ; * POINTER
  100.    If Var_TYPE = "*" Then
  101.        tempo = Left(line_input_file, Found_char - 2)
  102.        CALLING_example = CALLING_example + "ptr "
  103.     End If
  104.    
  105.    ; void
  106.    If Var_TYPE <> "#" And Var_TYPE <> "%" And Var_TYPE <> "$" And Var_TYPE <> "*" Then
  107.        tempo = Left(line_input_file, Found_char - 1)
  108.        CALLING_example = CALLING_example + "void "
  109.    End If
  110.    
  111.    
  112.    ; Nbr of parameters
  113.    If Mid(line_input_file, Found_char + 1, 1) = ")" Then
  114.        middle = ");"
  115.        Goto continue
  116.    End If
  117.    
  118.    ; parameters
  119.    ; remove the ( )
  120.    tempo_string = ""
  121.    nb_param = 0
  122.    For i = Found_char + 1 To 1000
  123.        car = Mid(line_input_file, i, 1)
  124.            If car = "," Then
  125.                nb_param = nb_param + 1
  126.                parameter(nb_param) = Trim(tempo_string)
  127.                car = ""
  128.                tempo_string = ""
  129.            End If
  130.            If car = ")" Then
  131.                nb_param = nb_param + 1
  132.                parameter(nb_param) = Trim(tempo_string)
  133.                tempo_string = ""
  134.                Exit
  135.            End If
  136.        tempo_string = tempo_string + car
  137.    Next
  138.    
  139.    ; parameters
  140.    middle = " "
  141.    For i = 1 To nb_param
  142.        ; % interger
  143.        If Right(parameter(i), 1) = "%" Then
  144.            ok = " int " + Left(parameter(i), Len(parameter(i)) - 1) + ","
  145.        End If
  146.        ; * pointer
  147.        If Right(parameter(i), 1) = "*" Then
  148.            ok = " System.IntPtr " + Left(parameter(i), Len(parameter(i)) - 1) + ","
  149.        End If
  150.                 ; # float
  151.        If Right(parameter(i), 1) = "#" Then
  152.            ok = " float " + Left(parameter(i), Len(parameter(i)) - 1) + ","
  153.        End If
  154.                 ; $ string
  155.        If Right(parameter(i), 1) = "$" Then
  156.            ok = " string " + Left(parameter(i), Len(parameter(i)) - 1) + ","
  157.        End If
  158.        middle = middle + ok
  159.        ok = ""
  160.    Next
  161.    ; remove the last ";"
  162.         If Len(middle)>1 Then middle=Trim(middle)
  163.    middle = Left(middle, Len(middle) - 1) + ");"
  164.  
  165. .continue
  166.  
  167.   WriteLine( file_out,Chr(9)+"// Original DECLS line ==> " + line_input_file)
  168.   WriteLine( file_out,CALLING_example + tempo + "(" + Trim(middle))
  169.  
  170. .suivant
  171. Wend
  172. ;
  173.  
  174. ;
  175. WriteLine(file_out,"")
  176. WriteLine(file_out,"")
  177. WriteLine(file_out,Chr(9)+"void Start ()")
  178. WriteLine(file_out,Chr(9)+"{")
  179. WriteLine(file_out,Chr(9)+"}")
  180. WriteLine(file_out,"}")
  181. CloseFile file_in
  182. CloseFile file_out
  183. End


Comments :


Rick Nasher(Posted 1+ years ago)

 The title surely sounds interesting and handy enough.I'm curious where one would use this for and why? I'm guessing this could this be used to port some generic Blitz dll's to Unity.. But Hardwired.dll for example and use it in C# (if one would want to do that, the abomination, the horror. lol) would that  be feasible/usable/compatible with Unity's very integrated  systems?Examples of usage perhaps?Questions, questions, questions.. [edit]Nevermind.. pff. Seeing it here(I must be shortsighted. Wait a second: I am! ;-)<a href="../Community/posts8e7c.html?topic=45509" target="_blank">Cheetah DB[/url]<a href="../Community/postsfa50.html?topic=104621" target="_blank">USERLIB DECLS TO CSharp (Unity 3D) converter[/url]


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal