CarlosLeyvaAyala / Papyrus-2-Typescript

Transforms *.psc files to *.ts
MIT License
12 stars 2 forks source link

Output has a bug (includes some Papyrus code) #2

Open mrowrpurr opened 2 years ago

mrowrpurr commented 2 years ago

When I ran the generator for JContainers 4.2.1, the JContainers.ts file included some Papyrus

Here is the relevant output:

// Returns true if JContainers plugin installed properly
export const isInstalled = (): boolean => sn.isInstalled()
    return __isInstalled() && 4 == APIVersion() && 2 == featureVersion()
endfunction

Here is the relevant error I got:

$ tsc
src/JContainers.ts:51:5 - error TS1108: A 'return' statement can only be used within a function body.

51     return __isInstalled() && 4 == APIVersion() && 2 == featureVersion()
       ~~~~~~

src/JContainers.ts:52:1 - error TS2304: Cannot find name 'endfunction'.

52 endfunction
   ~~~~~~~~~~~
mrowrpurr commented 2 years ago

I tried outputting the latest version of PapyrusUtil and had the same issue in a few places, e.g.

export const JsonExists = (FileName: string): boolean => sn.JsonExists(FileName)
    if !FileName
        return false
    elseIf StringUtil.Find(FileName, ".json") == -1
        FileName += ".json"
    endIf
    return MiscUtil.FileExists("data/skse/plugins/StorageUtilData/"+FileName)
endFunction
mrowrpurr commented 2 years ago

And then in some places I get a weird mix of TypeScript and Papyrus, e.g.

// ## Similar to SKSE's native StringUtil.Split() except results are whitespace trimmed. So comma, separated,list,can, be, spaced,or,not.
export const StringSplit = (// string ArgString, string Delimiter = ","): string[] => sn.StringSplit(ArgString,  Delimiter, ")

// ## Opposite of StringSplit()
export const StringJoin = (// string[] Values, string Delimiter = ","): string => sn.StringJoin(Values,  Delimiter, ")
CarlosLeyvaAyala commented 2 years ago

Yes.

This only translates function headers, not function bodies. Everything that is not a function header gets exported as is; in this case, plain Papyrus code.

PapyrusUtil was particularly hard and annoying to translate because of that.

Doing full translation from Papyrus to Typescript is way beyond what I'm willing to do xD. I would need to parse, tokenize and whatnot.
Too much work.

In fact, I had to manually translate all functions you listed.

CarlosLeyvaAyala commented 2 years ago

And then in some places I get a weird mix of TypeScript and Papyrus, e.g.

// ## Similar to SKSE's native StringUtil.Split() except results are whitespace trimmed. So comma, separated,list,can, be, spaced,or,not.
export const StringSplit = (// string ArgString, string Delimiter = ","): string[] => sn.StringSplit(ArgString,  Delimiter, ")

// ## Opposite of StringSplit()
export const StringJoin = (// string[] Values, string Delimiter = ","): string => sn.StringJoin(Values,  Delimiter, ")

The new version I'm working on already deals with this sucessfully. Hope to be able to release around these days.

CarlosLeyvaAyala commented 2 years ago

I tried outputting the latest version of PapyrusUtil and had the same issue in a few places, e.g.

export const JsonExists = (FileName: string): boolean => sn.JsonExists(FileName)
  if !FileName
      return false
  elseIf StringUtil.Find(FileName, ".json") == -1
      FileName += ".json"
  endIf
  return MiscUtil.FileExists("data/skse/plugins/StorageUtilData/"+FileName)
endFunction

In the new version I added a mode to patch those kind of things once they have been manually translated. It deals with this function in particular and many others thanks to a json config file.