Skip to content

Commit 01d8297

Browse files
committed
feat: Sciter archive support.
Also refactor some C -> Go data conversions. refs sciter-sdk#86
1 parent e1aafc2 commit 01d8297

File tree

6 files changed

+60
-68
lines changed

6 files changed

+60
-68
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ Check [this page](http://sciter.com/developers/sciter-sdk-bindings/) for other l
1313
# Attention
1414

1515
The ownership of project is transferred to this new organization.
16-
Thus the `import path` for golang should now be `github.com/sciter-sdk/go-sciter`,
17-
but the package name is still `sciter`.
16+
Thus the `import path` for golang should now be `github.com/sciter-sdk/go-sciter`, but the package name is still `sciter`.
1817

1918
# Introduction
2019

@@ -66,6 +65,8 @@ Things that are not supported:
6665

6766
Under Linux gcc(4.8 or above) and gtk+-3.0 are needed.
6867

68+
At the moment only **Go 1.10** or higher is supported (issue #136).
69+
6970
4. `go get -x github.com/sciter-sdk/go-sciter`
7071

7172
5. Run the example and enjoy :)

sciter-x-api.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ const char * SCITER_DLL_PATH = SCITER_DLL_NAME;
5454
return _api;
5555
}
5656

57-
HSARCHIVE SCAPI SciterOpenArchive (LPCBYTE archiveData, UINT archiveDataLength) {
58-
return SAPI(NULL)->SciterOpenArchive (archiveData,archiveDataLength);
59-
}
60-
61-
BOOL SCAPI SciterGetArchiveItem (HSARCHIVE harc, LPCWSTR path, LPCBYTE* pdata, UINT* pdataLength){
62-
return SAPI(NULL)->SciterGetArchiveItem (harc,path,pdata,pdataLength);
63-
}
64-
65-
6657
#elif defined(OSX)
6758

6859
#include <string.h>
@@ -376,3 +367,15 @@ const char * SCITER_DLL_PATH = SCITER_DLL_NAME;
376367
BOOL SCAPI Sciter_V2v(HVM vm, const VALUE* value, tiscript_value* out_script_value) { return SAPI(NULL)->Sciter_V2v(vm,value,out_script_value); }
377368

378369
BOOL SCAPI SciterProcX(HWINDOW hwnd, SCITER_X_MSG* pMsg) { return SAPI(NULL)->SciterProcX(hwnd, pMsg); }
370+
371+
HSARCHIVE SCAPI SciterOpenArchive (LPCBYTE archiveData, UINT archiveDataLength) {
372+
return SAPI(NULL)->SciterOpenArchive (archiveData, archiveDataLength);
373+
}
374+
375+
BOOL SCAPI SciterGetArchiveItem (HSARCHIVE harc, LPCWSTR path, LPCBYTE* pdata, UINT* pdataLength) {
376+
return SAPI(NULL)->SciterGetArchiveItem (harc, path, pdata, pdataLength);
377+
}
378+
379+
BOOL SCAPI SciterCloseArchive (HSARCHIVE harc) {
380+
return SAPI(NULL)->SciterCloseArchive (harc);
381+
}

sciter.go

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ extern INT SC_CALLBACK ELEMENT_COMPARATOR_cgo( HELEMENT he1, HELEMENT he2, LPVOI
2626
extern BOOL SC_CALLBACK KeyValueCallback_cgo(LPVOID param, const VALUE* pkey, const VALUE* pval );
2727
2828
extern const char * SCITER_DLL_PATH;
29+
30+
extern HSARCHIVE SCAPI SciterOpenArchive (LPCBYTE archiveData, UINT archiveDataLength);
31+
32+
extern BOOL SCAPI SciterGetArchiveItem (HSARCHIVE harc, LPCWSTR path, LPCBYTE* pdata, UINT* pdataLength);
33+
34+
extern BOOL SCAPI SciterCloseArchive (HSARCHIVE harc);
35+
2936
*/
3037
import "C"
3138
import (
@@ -41,6 +48,8 @@ type Sciter struct {
4148
callbacks map[*CallbackHandler]struct{}
4249
// map scripting function name to NativeFunctor
4350
*eventMapper
51+
// sciter archive
52+
har C.HSARCHIVE
4453
}
4554

4655
var (
@@ -439,25 +448,34 @@ func (s *Sciter) SetHomeURL(baseUrl string) (ok bool) {
439448
}
440449
return true
441450
}
451+
452+
// Open a data blob of the Sciter compressed archive.
442453
func (s *Sciter) OpenArchive(data []byte) {
443-
    s.har = C.SciterOpenArchive((*C.BYTE)(&data[0]), C.UINT(len(data)))
454+
s.har = C.SciterOpenArchive((*C.BYTE)(&data[0]), C.UINT(len(data)))
444455
}
456+
457+
// Get an archive item referenced by \c uri.
458+
//
459+
// Usually it is passed to \c Sciter.DataReady().
445460
func (s *Sciter) GetArchiveItem(uri string) []byte {
446-
    var pv uintptr = 0
447-
    var length uint = 0
448-
    pBytes := (*C.LPCBYTE)(unsafe.Pointer(&pv))
449-
    pnBytes := (*C.UINT)(unsafe.Pointer(&length))
450-
    r := C.SciterGetArchiveItem(s.har, StringToWcharPtr(uri), pBytes, pnBytes)
451-
    if r == 0 {
452-
        return nil
453-
    }
454-
    ret := []byte{}
455-
    for i := 0; i < int(length); i++ {
456-
        b := *(*byte)(unsafe.Pointer(pv + uintptr(i)))
457-
        ret = append(ret, b)
458-
    }
459-
    return ret
461+
var data C.LPCBYTE
462+
var length C.UINT
463+
cdata := (*C.LPCBYTE)(&data)
464+
clength := C.LPUINT(&length)
465+
r := C.SciterGetArchiveItem(s.har, StringToWcharPtr(uri), cdata, clength)
466+
if r == 0 {
467+
return nil
468+
}
469+
ret := ByteCPtrToBytes(data, length)
470+
return ret
460471
}
472+
473+
// Close the archive.
474+
func (s *Sciter) CloseArchive() {
475+
C.SciterCloseArchive(s.har)
476+
s.har = C.HSARCHIVE(nil)
477+
}
478+
461479
// #if defined(OSX)
462480
// HWINDOW SciterCreateNSView ( LPRECT frame ) ;//{ return SAPI()->SciterCreateNSView ( frame ); }
463481
// #endif
@@ -664,14 +682,7 @@ func (e *Element) ParentElement() (*Element, error) {
664682

665683
//export goLPCBYTE_RECEIVER
666684
func goLPCBYTE_RECEIVER(bs *byte, n uint, param unsafe.Pointer) int {
667-
var r []byte
668-
p := uintptr(unsafe.Pointer(bs))
669-
println("goLPCBYTE_RECEIVER:", n)
670-
for i := 0; i < int(n); i++ {
671-
u := *(*byte)(unsafe.Pointer(p + uintptr(i)))
672-
r = append(r, u)
673-
}
674-
println("in:", string(r))
685+
r := BytePtrToBytes(bs, n)
675686
*(*[]byte)(param) = r
676687
return 0
677688
}
@@ -2021,21 +2032,16 @@ func (pdst *Value) SetFloat(data float64) error {
20212032
func (pdst *Value) Bytes() []byte {
20222033
cpdst := (*C.VALUE)(unsafe.Pointer(pdst))
20232034
// args
2024-
var pv uintptr = 0
2025-
var length uint = 0
2035+
var pv C.LPCBYTE
2036+
var length C.UINT
20262037
pBytes := (*C.LPCBYTE)(unsafe.Pointer(&pv))
2027-
pnBytes := (*C.UINT)(unsafe.Pointer(&length))
2038+
pnBytes := C.LPUINT(&length)
20282039
// cgo call
20292040
r := C.ValueBinaryData(cpdst, pBytes, pnBytes)
20302041
if r != C.UINT(HV_OK) {
20312042
return nil
20322043
}
2033-
ret := []byte{}
2034-
sz := unsafe.Sizeof(pv)
2035-
for i := 0; i < int(length); i++ {
2036-
b := *(*byte)(unsafe.Pointer(pv + uintptr(i)*sz))
2037-
ret = append(ret, b)
2038-
}
2044+
ret := ByteCPtrToBytes(pv, length)
20392045
return ret
20402046
}
20412047

sciter_windows.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ func ProcND(hwnd win.HWND, msg uint, wParam uintptr, lParam uintptr) (ret int, h
2525
return
2626
}
2727

28-
//BOOL SciterLoadFile (HWINDOW hWndSciter, LPCWSTR filename) ;//{ return SAPI()->SciterLoadFile (hWndSciter,filename); }
29-
func LoadFileForHWND(hwnd win.HWND, filename string) error {
30-
31-
ret := C.SciterLoadFile(C.HWINDOW(unsafe.Pointer(hwnd)), StringToWcharPtr(filename))
32-
if ret == 0 {
33-
return fmt.Errorf("LoadFile with: %s failed", filename)
34-
}
35-
return nil
36-
}
37-
3828
// HWINDOW SciterCreateWindow ( UINT creationFlags,LPRECT frame, SciterWindowDelegate* delegate, LPVOID delegateParam, HWINDOW parent);
3929

4030
// Create sciter window.

types.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -887,14 +887,7 @@ func (s *ScnLoadData) Uri() string {
887887
}
888888

889889
func (s *ScnLoadData) Data() []byte {
890-
ret := []byte{}
891-
var b byte
892-
pdata := uintptr(unsafe.Pointer(s.outData))
893-
step := unsafe.Sizeof(b)
894-
for i := 0; i < int(s.outDataSize); i++ {
895-
b = *(*byte)(unsafe.Pointer(pdata + uintptr(i)*step))
896-
ret = append(ret, b)
897-
}
890+
ret := ByteCPtrToBytes(s.outData, s.outDataSize)
898891
return ret
899892
}
900893

utils.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ func StringToUTF16PtrWithLen(s string) (*uint16, int) {
6969
return &us[0], length
7070
}
7171

72+
func ByteCPtrToBytes(bp: C.LPCBYTE, size C.UINT) []byte {
73+
bs := C.GoBytes(unsafe.Pointer(data), C.INT(length))
74+
return bs
75+
}
76+
7277
func BytePtrToBytes(bp *byte, size uint) []byte {
73-
bs := []byte{}
74-
p := uintptr(unsafe.Pointer(bp))
75-
step := unsafe.Sizeof(*bp)
76-
for i := uint(0); i < size; i++ {
77-
b := *(*byte)(unsafe.Pointer(p + uintptr(i)*step))
78-
bs = append(bs, b)
79-
}
78+
bs := C.GoBytes(unsafe.Pointer(bp), C.INT(size))
8079
return bs
8180
}

0 commit comments

Comments
 (0)