vanilladocs
vanillapunch.com
API / resource/textasset

resource/textasset

글 애셋. (원본 resource/textasset.js, resource/jsonasset.js)

  • 원본 TextAsset 은 fetch 로 받은 글을 text 에, JsonAsset 은 JSON.parse 한 것을 data 에 둔다.
  • 러스트 판은 바이트가 다 오면 처음 꺼낼 때 UTF-8 글로 풀고, JsonAsset 은 serde_json::Value 로 푼다.
  • JsonAsset 은 원본처럼 TextAsset 의 글(get_text)도 들고, 풀지 못한 JSON 은 읽히지 않은 것(is_loaded 거짓, get_data None)이다. AssetLoader 로 받으면 원본처럼 실패 목록에 든다. (푼 자료는 AssetLoader 의 목록 칸과 나눠 들어 한 번만 푼다)
  • set_no_cache: 참이면 웹 fetch 에 cache: "no-cache" 를 붙여 배포 뒤 옛 자료를 쓰지 않게 한다. (원본과 같다, 데스크탑은 늘 파일을 새로 읽어 켠 것과 결과가 같다)
  • load 는 원본처럼 await 할 수 있는 읽기 손잡이를 돌려준다. (원본 JsonAsset.load 처럼 받기나 JSON 해석이 실패하면 await 가 실패로 끝나고 get_data 가 None 이다)

Text asset. (the original resource/textasset.js, resource/jsonasset.js)

  • The original TextAsset keeps the text received with fetch in text, and JsonAsset keeps the JSON.parse result in data.
  • The Rust version decodes it as UTF-8 text on first access once the bytes arrive, and JsonAsset decodes it into serde_json::Value.
  • Like the original, JsonAsset also holds TextAsset's text (get_text), and JSON that cannot be parsed counts as not loaded (is_loaded false, get_data None). When received by AssetLoader it goes into the failure list as in the original. (the parsed data is shared with the AssetLoader list entry so it is parsed only once)
  • set_no_cache: if true, adds cache: "no-cache" to web fetch so old data is not used after a deploy. (same as the original, desktop always reads the file anew, so the result is the same as on)
  • load returns a load handle that can be awaited as in the original. (like the original JsonAsset.load, if receiving or JSON parsing fails, the await ends in failure and get_data is None)

TextAssetstruct traits Default, Asset

짝Counterpart: vanilla.js TextAsset소스source

글 애셋.

  • 바이트가 다 오면 처음 get_text 할 때 UTF-8 글로 푼다.

Text asset.

  • Once the bytes arrive, decodes them as UTF-8 text on the first get_text.
예제Exampledata: 주소와 가진 바이트로 읽기Read from a data: URL and from bytes in hand
use vanilla::{Engine, EngineConfiguration, TextAsset};

let engine = Engine::new(EngineConfiguration::new());
let mut text_asset = TextAsset::new();
text_asset.load(&engine, "data:,hello");
assert_eq!(text_asset.get_text(), Some("hello"));

let mut note_asset = TextAsset::new();
assert_eq!(note_asset.get_text(), None);
note_asset.load_from_bytes("note.txt", b"line one".to_vec());
assert_eq!(note_asset.get_text(), Some("line one"));
생성자constructorpub fn new() -> TextAsset#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

반환ReturnsTextAsset 요청 전의 빈 글 애셋. (캐시 무효화 꺼짐)Empty text asset before a request. (cache busting off)

짝Counterpart: vanilla.js TextAsset.constructor

메서드methodpub fn set_no_cache(&mut self, is_no_cache: bool)#0.4.0 부터since 0.4.0소스source

캐시 무효화 로드 여부 설정. (참이면 웹 fetch 에 cache: "no-cache" 를 붙인다, 데이터 표 JSON 등)

Sets whether to load with cache busting. (if true, adds cache: "no-cache" to web fetch, for data table JSON, etc.)

is_no_cachebool
true 면 웹에서 브라우저 캐시를 쓰지 않는다. (load 전에 정한다, 데스크탑은 늘 파일을 새로 읽어 켠 것과 같다)If true, does not use the browser cache on web. (set before load, desktop always reads the file anew, which is the same as on)

짝Counterpart: vanilla.js TextAsset.setNoCache

메서드methodpub fn is_no_cache(&self) -> bool#0.4.0 부터since 0.4.0소스source

캐시 무효화 로드 여부 반환.

Returns whether to load with cache invalidation.

반환Returnsbool 웹에서 브라우저 캐시를 쓰지 않으면 true. (기본 false)true if it does not use the browser cache on web. (default false)

짝Counterpart: vanilla.js TextAsset.isNoCache

메서드methodpub fn load(&mut self, engine: &Engine, asset_path: &str) -> AssetLoadHandle#0.3.2 부터since 0.3.2소스source

읽기 요청. (원본 load, await 할 수 있는 읽기 손잡이를 돌려준다)

  • 이미 요청했으면 다시 하지 않고 처음 손잡이를 돌려준다.

Requests a read. (the original load, returns a load handle that can be awaited)

  • If already requested, does not request again and returns the first handle.
engine&Engine
바이트를 부탁할 엔진.Engine to ask for the bytes.
asset_path&str
애셋 경로. (데스크탑은 애셋 폴더 기준, 웹은 asset_base_url 기준, data: 주소도 된다)Asset path. (relative to the asset folder on desktop, to asset_base_url on the web; data: URLs also work)

반환ReturnsAssetLoadHandle 읽기 손잡이. (데스크탑은 그 자리에서 읽혀 끝나 있다. 못 읽으면 실패로 끝난다)Load handle. (on desktop it is read on the spot and already finished. Finishes in failure if it cannot be read)

짝Counterpart: vanilla.js TextAsset.load

메서드methodpub fn load_from_bytes(&mut self, asset_path: &str, bytes: Vec<u8>)#0.3.2 부터since 0.3.2소스source

이미 가진 바이트로 읽기.

  • 앞서 푼 글은 버린다. 이미 요청한 손잡이가 있어도 새 손잡이로 바꾼다.

Reads from bytes already in hand.

  • Discards the previously decoded text. Replaces it with a new handle even if a requested handle exists.
asset_path&str
적어 둘 경로.Path to record.
bytesVec<u8>
글 바이트. (UTF-8)Text bytes. (UTF-8)

짝Counterpart: vanilla.js TextAsset.loadFromBytes

메서드methodpub fn unload(&mut self)#0.3.2 부터since 0.3.2소스source

내리기.

Unloads.

짝Counterpart: vanilla.js TextAsset.unload

메서드methodpub fn get_text(&mut self) -> Option<&str>#0.3.2 부터since 0.3.2소스source

글. (다 읽히기 전이면 None. UTF-8 이 아닌 바이트는 대체 문자로 바꾼다)

Text. (None before it is fully read. Bytes that are not UTF-8 are replaced with replacement characters)

반환ReturnsOption<&str> 읽은 글. 요청 전이거나 읽는 중이거나 실패했으면 None.Text read. None before a request, while reading, or if it failed.

짝Counterpart: vanilla.js TextAsset.text

JsonAssetstruct traits Default, Asset

짝Counterpart: vanilla.js JsonAsset소스source

JSON 애셋.

  • 바이트가 다 오면 처음 볼 때 serde_json::Value 로 한 번 푼다. 풀지 못한 JSON 은 읽히지 않은 것(is_loaded 거짓, get_data None)이고 글은 남는다.

JSON asset.

  • Once the bytes arrive, parses them once into serde_json::Value on first look. JSON that cannot be parsed counts as not loaded (is_loaded false, get_data None) and the text remains.
예제Example저장소의 아틀라스 JSON 을 읽어 값 보기Read atlas JSON from the repository and check its values
use vanilla::{Asset, Engine, EngineConfiguration, JsonAsset};

let engine = Engine::new(EngineConfiguration::new());
let mut json_asset = JsonAsset::new();
json_asset.set_no_cache(true);
json_asset.load(&engine, "examples/assets/atlas/icons.json");
assert!(json_asset.is_loaded());
let data = json_asset.get_data().expect("json data");
assert_eq!(data["meta"]["imageFileName"], "icons.png");
assert_eq!(data["images"]["coin.png"]["width"], 30);
예제Example풀지 못한 JSON 은 읽히지 않은 것이고 글은 남는다JSON that cannot be parsed counts as not loaded and the text remains
use vanilla::{Asset, JsonAsset};

let mut json_asset = JsonAsset::new();
json_asset.load_from_bytes("broken.json", b"{\"count\": ".to_vec());
assert!(!json_asset.is_loaded());
assert!(json_asset.get_data().is_none());
assert_eq!(json_asset.get_text(), Some("{\"count\": "));
생성자constructorpub fn new() -> JsonAsset#0.3.2 부터since 0.3.2소스source

생성.

Constructor.

반환ReturnsJsonAsset 요청 전의 빈 JSON 애셋. (캐시 무효화 꺼짐)Empty JSON asset before a request. (cache busting off)

짝Counterpart: vanilla.js JsonAsset.constructor

메서드methodpub fn set_no_cache(&mut self, is_no_cache: bool)#0.4.0 부터since 0.4.0소스source

캐시 무효화 로드 여부 설정. (참이면 웹 fetch 에 cache: "no-cache" 를 붙인다, 데이터 표 JSON 등)

Sets whether to load with cache busting. (if true, adds cache: "no-cache" to web fetch, for data table JSON, etc.)

is_no_cachebool
true 면 웹에서 브라우저 캐시를 쓰지 않는다. (load 전에 정한다, 데스크탑은 늘 파일을 새로 읽어 켠 것과 같다)If true, does not use the browser cache on web. (set before load, desktop always reads the file anew, which is the same as on)

짝Counterpart: vanilla.js TextAsset.setNoCache

메서드methodpub fn is_no_cache(&self) -> bool#0.4.0 부터since 0.4.0소스source

캐시 무효화 로드 여부 반환.

Returns whether to load with cache invalidation.

반환Returnsbool 웹에서 브라우저 캐시를 쓰지 않으면 true. (기본 false)true if it does not use the browser cache on web. (default false)

짝Counterpart: vanilla.js TextAsset.isNoCache

메서드methodpub fn load(&mut self, engine: &Engine, asset_path: &str) -> AssetLoadHandle#0.3.2 부터since 0.3.2소스source

읽기 요청. (원본 load, await 할 수 있는 읽기 손잡이를 돌려준다. 원본처럼 받기나 JSON 해석이 실패하면 await 가 실패로 끝나고 get_data 가 None 이다)

Requests a load. (the original load, returns a load handle that can be awaited. As in the original, if receiving or JSON parsing fails, the await ends in failure and get_data is None)

engine&Engine
바이트를 부탁할 엔진.Engine to ask for the bytes.
asset_path&str
애셋 경로. (데스크탑은 애셋 폴더 기준, 웹은 asset_base_url 기준, data: 주소도 된다)Asset path. (relative to the asset folder on desktop, to asset_base_url on the web; data: URLs also work)

반환ReturnsAssetLoadHandle 읽기 손잡이. (데스크탑은 그 자리에서 읽혀 끝나 있다. 못 읽으면 실패로 끝난다)Load handle. (on desktop it is read on the spot and already finished. Finishes in failure if it cannot be read)

짝Counterpart: vanilla.js JsonAsset.load

메서드methodpub fn load_from_bytes(&mut self, asset_path: &str, bytes: Vec<u8>)#0.3.2 부터since 0.3.2소스source

이미 가진 바이트로 읽기.

  • 앞서 푼 자료와 글은 버리고 새로 푼다. 이미 요청한 손잡이가 있어도 새 손잡이로 바꾼다.

Reads from bytes already in hand.

  • Discards the previously parsed data and text and parses anew. Replaces it with a new handle even if a requested handle exists.
asset_path&str
적어 둘 경로. (풀지 못했을 때 알림 글에 쓴다)Path to record. (used in notice text when it cannot be parsed)
bytesVec<u8>
JSON 글 바이트. (UTF-8)JSON text bytes. (UTF-8)

짝Counterpart: vanilla.js JsonAsset.loadFromBytes

메서드methodpub fn unload(&mut self)#0.3.2 부터since 0.3.2소스source

내리기.

Unloads.

짝Counterpart: vanilla.js JsonAsset.unload

메서드methodpub fn get_data(&self) -> Option<&serde_json::Value>#0.3.2 부터since 0.3.2소스source

자료. (원본 data. 다 읽히기 전이거나 해석에 실패하면 None)

Data. (the original data. None before it is fully read or if parsing fails)

반환ReturnsOption<&serde_json::Value> 푼 JSON 값. 요청 전이거나 읽는 중이거나 읽기나 풀기에 실패했으면 None.Parsed JSON value. None before a request, while reading, or if reading or parsing failed.

짝Counterpart: vanilla.js JsonAsset.data

메서드methodpub fn get_text(&mut self) -> Option<&str>#0.4.0 부터since 0.4.0소스source

글. (원본 TextAsset 의 text. 다 읽히기 전이면 None, 풀지 못한 JSON 도 글은 있다. UTF-8 이 아닌 바이트는 대체 문자로 바꾼다)

Text. (text of the original TextAsset. None before it is fully read, JSON that cannot be parsed still has text. Bytes that are not UTF-8 are replaced with replacement characters)

반환ReturnsOption<&str> 읽은 JSON 글. 요청 전이거나 읽는 중이거나 읽기에 실패했으면 None.JSON text read. None before a request, while reading, or if reading failed.

짝Counterpart: vanilla.js TextAsset.text