touilleMan / godot-python

Python support for Godot 🐍🐍🐍
Other
1.86k stars 140 forks source link

How to export windows platform package #275

Open vmjcv opened 3 years ago

vmjcv commented 3 years ago

I read this article,https://github.com/touilleMan/godot-python/issues/146

But I still don’t know how to export to the windows platform. I hope to build a commercial game through this, and I don’t want to expose the source code.

If you plan to provide an export tool, can you tell me the approximate time? Affect my game development plan

Or other manual export methods are acceptable, but unfortunately I read most of the articles and did not get a satisfactory answer

vmjcv commented 3 years ago

I want to know Why look for dll in addons instead of in the project folder image

touilleMan commented 3 years ago

你好 @vmjcv ;-)

But I still don’t know how to export to the windows platform.

Export is basically a fancy word for:

Export doesn't currently work with project using Godot-Python given Godot doesn't know how to package .py (and the python interpreter, and also the python native modules) files into the bundle.

However you don't need to use the export to distribute you game: simply add the godot binary to your game project should be enough.

Of course this is not that simple given you game project most likely contains assets that shouldn't be part of the final build. So instead of doing the triage by hand, you can (see my original comment https://github.com/touilleMan/godot-python/issues/146#issue-558880160):

This way your start by asking Godot to export everything it knows about. Then you patch the export to add the Godot-Python stuff ;-)

I don’t want to expose the source code.

You can do this with the python compileall module (https://docs.python.org/3/library/compileall.html) Basically in python your .py files got compiled into a .pyc file before being actually executed. This is normally automatically done at runtime, but you can do the compilation by hand:

$ echo "print('hello')" > hello.py
$ python hello.py
hello
$ python -m compileall -b hello.py
Compiling 'hello.py'...
$ ls
hello.py  hello.pyc
$ rm hello.py
$ python hello.pyc
hello
$ hexdump -C hello.pyc
00000000  33 0d 0d 0a 70 46 36 60  0f 00 00 00 e3 00 00 00  |3...pF6`........|
00000010  00 00 00 00 00 00 00 00  00 02 00 00 00 40 00 00  |.............@..|
00000020  00 73 0c 00 00 00 65 00  64 00 83 01 01 00 64 01  |.s....e.d.....d.|
00000030  53 00 29 02 5a 05 68 65  6c 6c 6f 4e 29 01 da 05  |S.).Z.helloN)...|
00000040  70 72 69 6e 74 a9 00 72  02 00 00 00 72 02 00 00  |print..r....r...|
00000050  00 fa 08 68 65 6c 6c 6f  2e 70 79 da 08 3c 6d 6f  |...hello.py..<mo|
00000060  64 75 6c 65 3e 01 00 00  00 73 00 00 00 00        |dule>....s....|
0000006e

So once compiled, you can only ship the .pyc (that contains binary bitecode)

vmjcv commented 3 years ago

Thank you very much for your prompt reply, but according to your operation, the scripts and resources of godot will also be exposed directly(.gd or .tres),Is that right?@touilleMan

vmjcv commented 3 years ago

This way your start by asking Godot to export everything it knows about. Then you patch the export to add the Godot-Python stuff ;-)

In fact, I have completed this operation through a script plug-in. The problem is that if it is not distributed through pck, will encryption and resource compression be a problem?

this is code

tool
extends EditorPlugin

class PythonExportPlugin:
    extends EditorExportPlugin

    func scan(path:String) -> Array:
        var file_name := ""
        var files := []
        var dir := Directory.new()
        if dir.open(path) != OK:
            print("Failed to open:"+path)
        else:
            dir.list_dir_begin(true)
            file_name = dir.get_next()
            while file_name!="":
                if dir.current_is_dir():
                    var sub_path = path+"/"+file_name
                    files += scan(sub_path)
                else:
                    var name := path+"/"+file_name
                    files.push_back(name)
                file_name = dir.get_next()
            dir.list_dir_end()
        return files

    func _add_python_addons() -> void:
        var files = scan('res://addons/pythonscript/windows-64')
        for python_file in files:
            var file = File.new()
            file.open(python_file, File.READ)
            var buff = file.get_buffer(file.get_len())
            file.close()
            add_file(python_file,buff,false)

    func _process_hooks(hooks_path: String, args: Array) -> void:
        var dir = Directory.new()
        if dir.open(hooks_path) == OK:
            dir.list_dir_begin()
            var file_name = dir.get_next()
            while file_name != '':
                if not dir.current_is_dir() and file_name.ends_with('.gd'):
                    var hook = load(dir.get_current_dir() + "/" + file_name)
                    hook.callv('process', args)
                file_name = dir.get_next()
        else:
            # ignore error
            pass

    func _process_start_hooks(features: PoolStringArray, debug: bool, path: String, flags: int) -> void:
        _process_hooks('res://addons/python-export/start_hook', [features, debug, path, flags])

    func _process_end_hooks() -> void:
        _process_hooks('res://addons/python-export/end_hook', [])

    func _export_begin(features: PoolStringArray, debug: bool, path: String, flags: int) -> void:
        _process_start_hooks(features, debug, path, flags)
        if 'Windows' in features:
            # Only handle the windows platform, ignore other platforms. Note that if you need to export to other platforms, you may need to close the plug-in and restart the client before exporting
            _add_python_addons()

    func _export_end() -> void:
        _process_end_hooks()
        pass

func _init():
    add_export_plugin(PythonExportPlugin.new())

func _enter_tree():
    pass

func _exit_tree():
    pass
vmjcv commented 3 years ago

As I got deeper, I probably made a tool for window export. Now the problem I encountered is that the encrypted pyc file cannot be directly read by Godot. How should I deal with this problem?@touilleMan

Andrea-Miele commented 3 years ago

@touilleMan is there any updates about this? @vmjcv Can you share the source code of all the files in the directory /addons/python-export?