1 | /********************************************************************** |
2 | * |
3 | * Zip container manager |
4 | * |
5 | * Copyright (c) 2021 Dario Deledda. All rights reserved. |
6 | * Use of this source code is governed by an MIT license |
7 | * that can be found in the LICENSE file. |
8 | * |
9 | * TODO: |
10 | **********************************************************************/ |
11 | import sokol.gfx |
12 | import szip |
13 | |
14 | fn (mut il Item_list) scan_zip(path string, in_index int) ! { |
15 | println('Scanning ZIP [${path}]') |
16 | mut zp := szip.open(path, szip.CompressionLevel.no_compression, szip.OpenMode.read_only)! |
17 | n_entries := zp.total()! |
18 | // println(n_entries) |
19 | for index in 0 .. n_entries { |
20 | zp.open_entry_by_index(index)! |
21 | is_dir := zp.is_dir()! |
22 | name := zp.name() |
23 | size := zp.size() |
24 | // println("$index ${name} ${size:10} $is_dir") |
25 | |
26 | if !is_dir { |
27 | ext := get_extension(name) |
28 | if is_image(ext) == true { |
29 | il.n_item += 1 |
30 | mut item := Item{ |
31 | need_extract: true |
32 | path: path |
33 | name: name.clone() |
34 | container_index: in_index |
35 | container_item_index: index |
36 | i_type: ext |
37 | n_item: il.n_item |
38 | drawable: true |
39 | size: size |
40 | } |
41 | il.lst << item |
42 | } |
43 | } |
44 | // IMPORTANT NOTE: don't close the zip entry before we have used all the items!! |
45 | zp.close_entry() |
46 | } |
47 | zp.close() |
48 | } |
49 | |
50 | fn (mut app App) load_texture_from_zip() !(gfx.Image, int, int) { |
51 | item := app.item_list.lst[app.item_list.item_index] |
52 | // println("Load from zip [${item.path}]") |
53 | |
54 | // open the zip |
55 | if app.zip_index != item.container_index { |
56 | if app.zip_index >= 0 { |
57 | app.zip.close() |
58 | } |
59 | app.zip_index = item.container_index |
60 | // println("Opening the zip [${item.path}]") |
61 | app.zip = szip.open(item.path, szip.CompressionLevel.no_compression, szip.OpenMode.read_only)! |
62 | } |
63 | // println("Now get the image") |
64 | app.zip.open_entry_by_index(item.container_item_index)! |
65 | zip_entry_size := int(item.size) |
66 | |
67 | app.resize_buf_if_needed(zip_entry_size) |
68 | |
69 | app.zip.read_entry_buf(app.mem_buf, app.mem_buf_size)! |
70 | app.zip.close_entry() |
71 | return app.load_texture_from_buffer(app.mem_buf, zip_entry_size) |
72 | } |